麻烦解释下下面的程序:
- public class TestThread {
- public static void main(String[] args) {
- ThreadB b = new ThreadB();
- ThreadC c = new ThreadC();
-
- c.setName("第二线程");
- b.setName("第一线程");
-
- c.start();
- System.out.println(Thread.currentThread().getName() + " is start ....");
- synchronized (c) {
- try {
- System.out.println("Waiting for b1 to complete.....");
- c.wait();
- System.out.println("Completed.now back to " + Thread.currentThread().getName());
- b.start();
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- }
- }
- class ThreadB extends Thread {
- int total;
-
- public void run() {
- synchronized (this) {
- System.out.println(Thread.currentThread().getName() + " is running...");
-
- for (int i = 0; i < 10; i++) {
- total += i;
- }
-
- System.out.println("total is " + total);
- }
- }
- }
- class ThreadC extends Thread {
- int sum = 1;
-
- public void run() {
- synchronized (this) {
- System.out.println(Thread.currentThread().getName() + " is running...");
-
- for (int i = 1; i < 10; i++) {
- sum *= i;
- }
-
- System.out.println("sum is " + sum);
- notify();
- }
- }
- }
复制代码 |
|