package thread1;
// this program is puzzle when run on 2 core ,I do not know why ,it just
// do not stopclass ThreadA {
publicstaticvoid main(String[] args) {
ThreadB b =new ThreadB();
b.start();
System.out.println("b is start....");
synchronized (b)// 括号里的b是什么意思,起什么作用? {
try {
System.out.println("Waiting for b to complete...");
b.wait();// 这一句是什么意思,究竟让谁wait? System.out.println("Completed.Now back to main thread");
} catch (InterruptedException e) {
System.out.println("InterruptedException");
}
}
System.out.println("main:Total is :"+ b.total);
}
}
class ThreadB extends Thread {
int total;
publicvoid run() {
synchronized (this) {
System.out.println("ThreadB is running..");
for (int i =0; i <10; i++) {
total += i;
System.out.println("total is "+ total);
}
notify();
/*
* try { System.out.println("b is waiting" ); wait();
* } catch (InterruptedException e) { System.out.println("InterruptedException"); }
*/
}
System.out.println("AFTER notify");
System.exit(0);
}
}
package thread1;
// this program is puzzle when run on 2 core ,I do not know why ,it just
// do not stopclass ThreadA {
publicstaticboolean tic =false;
publicstaticvoid main(String[] args) {
ThreadB b =new ThreadB();
b.start();
System.out.println("b is start....");
while (true) {
while (ThreadA.tic ==false) {
synchronized (b)// 括号里的b是什么意思,起什么作用? {
try {
System.out.println("Waiting for b to complete...");
b.wait();// 这一句是什么意思,究竟让谁wait? System.out.println("Completed.Now back to main thread");
} catch (InterruptedException e) {
System.out.println("InterruptedException");
}
}
}
if (ThreadA.tic ==true) {
System.out.println("main:FINISH");
System.exit(4);
}
}
// System.out.println("main:Total is :" + b.total); }
}
class ThreadB extends Thread {
int total;
publicvoid run() {
System.out.println("ThreadB is running..");
for (int i =0; i <100; i++) {
total += i;
System.out.println("total is "+ total);
}
synchronized (this) {
ThreadA.tic =true;
notify();
/*
* try { System.out.println("b is waiting" ); wait();// 这一句是什么意思,究竟让谁wait? } catch (InterruptedException e) {
* System.out.println("InterruptedException"); }
*/
}
System.out.println("AFTER notify");
System.exit(0);
}
}
package thread1;
//this program is puzzle when run on 2 core ,I do not know why ,it just
// do not stopclass ThreadA {
publicstaticboolean tic =false;
publicstaticvoid main(String[] args) {
ThreadB b =new ThreadB();
b.start();
int i=1;
System.out.println("b is start....");
while (true)
{
while (ThreadA.tic ==false) {
synchronized (b)// 括号里的b是什么意思,起什么作用? {
try {
System.out.println("Waiting for b to complete...");
b.wait();// 这一句是什么意思,究竟让谁wait? System.out.println("Completed.Now back to main thread");
} catch (InterruptedException e) {
System.out.println("InterruptedException");
}
}
}
System.out.println("loop in while true for"+i+"times");
if (ThreadA.tic ==true) {
System.out.println("main:FINISH");
System.exit(4);
}
}
// System.out.println("main:Total is :" + b.total); }
}
class ThreadB extends Thread {
int total;
publicvoid run() {
System.out.println("ThreadB is running..");
for (int i =0; i <100; i++) {
total += i;
System.out.println("total is "+ total);
}
synchronized (this) {
ThreadA.tic =true;
notify();
/*
* try { System.out.println("b is waiting" ); wait();//
* 这一句是什么意思,究竟让谁wait?
* } catch (InterruptedException e) {
* System.out.println("InterruptedException"); }
*/
}
System.out.println("AFTER notify");
System.exit(0);
}
}