62,615
社区成员
发帖
与我相关
我的任务
分享
public class ThreadTest extends Thread {
public void run() {
while(true){
System.out.println(this.getName());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
ThreadTest t = new ThreadTest();
t.start();
t.wait();//这样是错的
}
}
public class ThreadTest extends Thread {
public void run() {
while(true){
System.out.println(this.getName());
this.pause();
}
}
public synchronized void pause(){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
ThreadTest t = new ThreadTest();
t.start();
t.pause();
}
}
public class ThreadTest extends Thread {
public void run() {
while(true){
System.out.println(this.getName());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void pause(){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
ThreadTest t = new ThreadTest();
t.start();
t.pause();
}
}