class NewThread implements Runnable {
String name;
Thread t;
boolean suspendFlag;
NewThread(String threadname) {
name=threadname;
t=new Thread(this,name);
System.out.println("New thread: "+t);
suspendFlag=false;
t.start();
}
public void run() {
try {
for(int i=15;i>0;i--) {
System.out.println(name+": "+i);
Thread.sleep(200);
synchronized(this) {
while(suspendFlag) {
wait();
}
}
}
} catch(InterruptedException e) {
System.out.println(name+" interrupted.");
}
System.out.println(name+" exiting.");
}
void mysuspend() {
suspendFlag=true;
}
synchronized void myresume() {
suspendFlag=false;
notify();
}
}class SuspendResume {
public static void main(String args[]) {
NewThread ob1=new NewThread("One");
NewThread ob2=new NewThread("Two");
try {
Thread.sleep(1000);
ob1.mysuspend();
System.out.println("Suspending thread One");
Thread.sleep(1000);
ob1.myresume();
System.out.println("Resuming thread One");
ob2.mysuspend();
System.out.println("Suspending thread Two");
Thread.sleep(1000);
ob2.myresume();
System.out.println("Resuming thread Two");
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
String name;
Thread t;
boolean suspendFlag;
NewThread(String threadname) {
name=threadname;
t=new Thread(this,name);
System.out.println("New thread: "+t);
suspendFlag=false;
t.start();
}
public void run() {
try {
for(int i=15;i>0;i--) {
System.out.println(name+": "+i);
Thread.sleep(200);
synchronized(this) {
while(suspendFlag) {
wait();
}
}
}
} catch(InterruptedException e) {
System.out.println(name+" interrupted.");
}
System.out.println(name+" exiting.");
}
void mysuspend() {
suspendFlag=true;
}
synchronized void myresume() {
suspendFlag=false;
notify();
}
}class SuspendResume {
public static void main(String args[]) {
NewThread ob1=new NewThread("One");
NewThread ob2=new NewThread("Two");
try {
Thread.sleep(1000);
ob1.mysuspend();
System.out.println("Suspending thread One");
Thread.sleep(1000);
ob1.myresume();
System.out.println("Resuming thread One");
ob2.mysuspend();
System.out.println("Suspending thread Two");
Thread.sleep(1000);
ob2.myresume();
System.out.println("Resuming thread Two");
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
解决方案 »
- JAVA3D问题求救,Text3D文字大小怎么调小啊
- 判断存到BufferReader里的email地址
- 一道java面试试题(考场上没作对气死了)
- 方法调用问题
- 请教高手:如何在java中制作一个TopMost窗口
- 使用jni调用ole对象问题,不考虑jacob
- 调用prepareStatement执行SQL语句的问题!!!!!!!!!!
- 一个jdbc statement的问题?
- jdk1.3 Error: /usr/bin/cut:no sub file or diretory. why?
- 对象的创建
- Java在现有Excel中追加数据的问题
- 使用API函数显示的对话框退出时导致程序一起退出
while(suspendFlag) {
wait();
}
}
这里的synchronized(this) 的作用
因为下面定义了一个同步方法,同步方法默认也是对this对象加锁
这样说了你应该明白了吧?
class NewThread implements Runnable {
String name;
Thread t;
boolean suspendFlag;
NewThread(String threadname) {
name=threadname;
t=new Thread(this,name);
System.out.println("New thread: "+t);
suspendFlag=false;
t.start();
}
public void run() {
try {
for(int i=15;i>0;i--) {
System.out.println(name+": "+i);
Thread.sleep(200);
synchronized(this) {
while(suspendFlag) {
wait();
}
}
}
} catch(InterruptedException e) {
System.out.println(name+" interrupted.");
}
System.out.println(name+" exiting.");
}
void mysuspend() {
suspendFlag=true;
}
synchronized void myresume() {
suspendFlag=false;
notify();
}
} class SuspendResume {
public static void main(String args[]) {
NewThread ob1=new NewThread("One");
NewThread ob2=new NewThread("Two");
try {
Thread.sleep(1000);
ob1.mysuspend();
System.out.println("Suspending thread One");
Thread.sleep(1000);
ob1.myresume();
System.out.println("Resuming thread One");
ob2.mysuspend();
System.out.println("Suspending thread Two");
Thread.sleep(1000);
ob2.myresume();
System.out.println("Resuming thread Two");
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
是为了防止suspendFlag变量被同时访问,
是为了多线程安全考虑的,否则如果多个线程修改和读取同时出现的时候
while(suspendFlag)
这一行就可能做出错误的判断synchronized的意思可以到网上 google