JAVA 多线程的问题

原地址:http://www.iteye.com/problems/92028

JAVA 多线程的问题0

Java代码 复制代码 收藏代码
  1. public class ReaderResult extends Thread {  
  2.     Calculator c;  
  3.  
  4.     public ReaderResult(Calculator c) {  
  5.             this.c = c;  
  6.     }  
  7.  
  8.     public void run() {  
  9.             synchronized (c) {  
  10.                     try {  
  11.                             System.out.println(Thread.currentThread() + "等待计算结果。。。");  
  12.                             c.wait();  
  13.                     } catch (InterruptedException e) {  
  14.                             e.printStackTrace();  
  15.                     }  
  16.                     System.out.println(Thread.currentThread() + "计算结果为:" + c.total);  
  17.             }  
  18.     }  
  19.  
  20.     public static void main(String[] args) {  
  21.             Calculator calculator = new Calculator();  
  22.             //启动三个线程,分别获取计算结果  
  23.             new ReaderResult(calculator).start();  
  24.             new ReaderResult(calculator).start();  
  25.             new ReaderResult(calculator).start();  
  26.             //启动计算线程  
  27.             calculator.start();  
  28.              
  29.     }  
public class ReaderResult extends Thread { 
    Calculator c; 

    public ReaderResult(Calculator c) { 
            this.c = c; 
    } 

    public void run() { 
            synchronized (c) { 
                    try { 
                            System.out.println(Thread.currentThread() + "等待计算结果。。。"); 
                            c.wait(); 
                    } catch (InterruptedException e) { 
                            e.printStackTrace(); 
                    } 
                    System.out.println(Thread.currentThread() + "计算结果为:" + c.total); 
            } 
    } 

    public static void main(String[] args) { 
            Calculator calculator = new Calculator(); 
            //启动三个线程,分别获取计算结果 
            new ReaderResult(calculator).start(); 
            new ReaderResult(calculator).start(); 
            new ReaderResult(calculator).start(); 
            //启动计算线程 
            calculator.start(); 
            
    } 
}

Java代码 复制代码 收藏代码
  1. public class Calculator extends Thread {  
  2.     int total;  
  3.     public void run() {  
  4.             synchronized (this) {  
  5.                 System.out.println(total); 
  6.                     for (int i = 0; i < 101; i++) {  
  7.                             total += i;  
  8.                     }  
  9.                     System.out.println(total);     
  10.             }  
  11.     }  
public class Calculator extends Thread { 
    int total; 
    public void run() { 
            synchronized (this) { 
            	System.out.println(total);
                    for (int i = 0; i < 101; i++) { 
                            total += i; 
                    } 
                    System.out.println(total);    
            } 
    } 
}


就2个简单的类 , 以我的认识,应该在c.wait()这里卡住,也就是说应该不会输出“计算结果为:" ”这些话。  但是为什么他有时候会输出呢?(有时候不会)。 到底是什么破坏了wait 状态?  应该不是start, 因为有时候是先输出System.out.println(total)这里, 然后再打印等待计算结果。。  高人请指点下
5 小时前

1个答案      按时间排序按投票排序

0 0

你将第一个线程类中的同步关键字c换成其他的对象试一下,如object对象。。看能否得到预期的结果?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值