lable
값을 0에서 n까지 증가시키는 스레드 클래스가 있습니다. 스레드 코드 : stop
값이 true
이 아닌 때까지스레드주기에서 단계를 만드는 방법은 무엇입니까?
public class
SimpleThread implements Runnable {
Thread t;
volatile boolean stop = true;
int n;
JLabel label;
String name;
SimpleThread(String name) {
this.name = name;
t = new Thread(this, "settingThread");
t.start();
}
public void run() {
while (true) {
while (stop) {
}
System.out.println(name + " stop:" + stop);
for (int i = 0; i < n; i++) {
label.setText(String.valueOf(i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
stop = true;
}
}
public void start(int n, JLabel label) {
stop = false;
this.n = n;
this.label = label;
}
}
스레드 작업해야합니다. 또한 나는 다른 인수와 함께 2 스레드 객체 st1
및 st2
있어 :
st1.start(10, timeLabel1);
st2.start(5, timeLabel2);
그래서 첫 번째 스레드가 0 ~ 10과 st2
에서 timeLabel1
를 증가한다 - 0에서 지금 5. 그리고 질문에. 싸이클에서 쓰레드가 무대를 통과 할 때까지 어떻게 프로그램을 대기시킬 수 있습니까? 다음
public void run() {
while (true) {
while (stop) {
}
System.out.println(name + " stop:" + stop);
for (int i = 0; i < n; i++) {
label.setText(String.valueOf(i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
stop = true;
//stage
}
}
그리고 : :이 같은 의미
st1.start(10, timeLabel1);
st2.start(5, timeLabel2);
//wait until both stages aren't done
System.out.println("yep");
코드 :
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Created with IntelliJ IDEA.
* User: Администратор
* Date: 22.11.13
* Time: 21:17
* To change this template use File | Settings | File Templates.
*/
public class StackOverflow implements ActionListener {
JButton button = new JButton("Button");
SimpleThread st1 = new SimpleThread("1st"), st2 = new SimpleThread("2nd");
JLabel timeLabel1 = new JLabel("0"), timeLabel2 = new JLabel("0");
StackOverflow() {
JFrame jfrm = new JFrame("Strategy fighting");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(300, 150);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(this);
jfrm.add(button);
jfrm.add(timeLabel1);
jfrm.add(timeLabel2);
jfrm.setVisible(true);
}
public class SimpleThread implements Runnable {
Thread t;
volatile boolean stop = true;
int n;
JLabel label;
String name;
SimpleThread(String name) {
this.name = name;
t = new Thread(this, "settingThread");
t.start();
}
public void run() {
while (true) {
while (stop) {
}
System.out.println(name + " stop:" + stop);
for (int i = 0; i < n; i++) {
label.setText(String.valueOf(i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
stop = true;
}
}
public void start(int n, JLabel label) {
stop = false;
this.n = n;
this.label = label;
}
}
public void actionPerformed(ActionEvent e) {
st1.start(10, timeLabel1);
st2.start(5, timeLabel2);
System.out.println("yep");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new StackOverflow();
}
});
}
}
대기를하고 할 수 있어야 통지 이 작업을 수행. http://www.programcreek.com/2009/02/notify-and-wait-example/ –
코드가 너무 많고 원하는 내용이 너무 적습니다. Лениво разбираться. –