A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 柳 德 彬 中级黑马   /  2013-4-17 20:54  /  1608 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 柳 德 彬 于 2013-4-17 21:57 编辑

一个增加的线程,一个减少的线程,启动四个线程实现加1减1 ,,求解,,最后什么也没有输出,,JVM一直在运行。。。。。???
//增加的 Thread
  1. public class Increase implements Runnable {
  2.         private TestThread2 obj;

  3.         public Increase(TestThread2 obj) {
  4.                 this.obj = obj;
  5.         }

  6.         public void run() {

  7.                 for (int i = 0; i < 20; i++) {
  8.                         try {
  9.                                 Thread.sleep((long) Math.random() * 1000);
  10.                         } catch (InterruptedException e) {
  11.                                 e.printStackTrace();
  12.                         }
  13.                         obj.inc();
  14.                 }
  15.         }
  16. }
复制代码
//减少的线程
  1. public class Decrease implements Runnable{
  2.         private TestThread2 obj;
  3.         public Decrease(TestThread2 obj){
  4.                 this.obj=obj;
  5.         }
  6.         public void run(){
  7.                 for (int i = 0; i < 20; i++) {
  8.                         try {
  9.                                 Thread.sleep((long)Math.random()*500);
  10.                         } catch (InterruptedException e) {
  11.                                 e.printStackTrace();
  12.                         }
  13.                         obj.dec();
  14.                 }
  15.         }
  16. }
复制代码
//加1减1的两个方法
  1. public class TestThread2 {
  2.         private int j;

  3.         public synchronized void inc() {
  4.                 if (0 != j) {
  5.                         try {
  6.                                 wait();
  7.                         } catch (InterruptedException e) {
  8.                                 e.printStackTrace();
  9.                         }
  10.                         j++;
  11.                         System.out.println(Thread.currentThread().getName() + "-inc:" + j);
  12.                         notify();
  13.                 }
  14.         }

  15.         public synchronized void dec() {
  16.                 if (0 == j) {
  17.                         try {
  18.                                 wait();
  19.                         } catch (InterruptedException e) {
  20.                                 e.printStackTrace();
  21.                         }
  22.                         j--;
  23.                         System.out.println(Thread.currentThread().getName() + "-dec:" + j);
  24.                         notify();
  25.                 }
  26.         }

  27. }
复制代码
// 测试
  1. public class MainClass {
  2.         public static void main(String[] args) {
  3.                 TestThread2 t = new TestThread2();
  4.                 Thread inc = new Thread(new Increase(t));
  5.                 Thread dec = new Thread(new Decrease(t));
  6.                 inc.start();
  7.                 dec.start();
  8.         }
  9. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

4 个回复

倒序浏览
写了那么多没回复成功,我真心郁闷了。
你分别在dec()和inc()中像我这样加入一个输出语句,一看就明白了。
最终的结果是,一个线程循环20次后,run结束,线程停止。
另外一个线程因为调用了wait,进入冻结状态。所以就没有输出,挂着不动了。
public synchronized void inc() {

                                System.out.println("-------111----->"+i++); //注意:要声明这个i变量,不能用j
                        if (0 != j) {
                                        try {
                                                        wait();
                                        } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                        }
                                        j++;
                                        System.out.println(Thread.currentThread().getName() + "-inc:" + j);
                                        notify();
                        }
        }

        public synchronized void dec() {
                        if (0 == j) {
                                System.out.println(Thread.currentThread().getName()+"------>222");
                                        try {
                                                        wait();
                                        } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                        }
                                        j--;
                                        System.out.println(Thread.currentThread().getName() + "-dec:" + j);
                                        notify();
                        }
        }
}
回复 使用道具 举报
写了那么多没回复成功,我真心郁闷了。
你分别在dec()和inc()中像我这样加入一个输出语句,一看就明白了。
最终的结果是,一个线程循环20次后,run结束,线程停止。
另外一个线程因为调用了wait,进入冻结状态。所以就没有输出,挂着不动了。
public synchronized void inc() {

                                System.out.println("-------111----->"+i++); //注意:要声明这个i变量,不能用j
                        if (0 != j) {
                                        try {
                                                        wait();
                                        } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                        }
                                        j++;
                                        System.out.println(Thread.currentThread().getName() + "-inc:" + j);
                                        notify();
                        }
        }

        public synchronized void dec() {
                        if (0 == j) {
                                System.out.println(Thread.currentThread().getName()+"------>222");
                                        try {
                                                        wait();
                                        } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                        }
                                        j--;
                                        System.out.println(Thread.currentThread().getName() + "-dec:" + j);
                                        notify();
                        }
        }
}

评分

参与人数 2技术分 +2 黑马币 +3 收起 理由
陈丽莉 + 2
柳 德 彬 + 3 赞一个!

查看全部评分

回复 使用道具 举报
//自增++         public synchronized void inc() {                  if (0 != j) {                         try {                                 this.wait();                         } catch (InterruptedException e) {                                 e.printStackTrace();                         }                 }//写错位置了。。                 j++;                 System.out.println(Thread.currentThread().getName() + "-inc:" + j);                 this.notify();         }         //自减--         public synchronized void dec() {                 if (0 == j) {                         try {                                 this.wait();                         } catch (InterruptedException e) {                                 e.printStackTrace();                         }                 }//写错位置了。。                 j--;                 System.out.println(Thread.currentThread().getName() + "-dec:" + j);                 this.notify();         }
回复 使用道具 举报
  1. //自增++
  2.         public synchronized void inc() {

  3.                 if (0 != j) {
  4.                         try {
  5.                                 this.wait();
  6.                         } catch (InterruptedException e) {
  7.                                 e.printStackTrace();
  8.                         }
  9.                 }//写错位置了。。
  10.                 j++;
  11.                 System.out.println(Thread.currentThread().getName() + "-inc:" + j);
  12.                 this.notify();
  13.         }
  14.         //自减--
  15.         public synchronized void dec() {
  16.                 if (0 == j) {
  17.                         try {
  18.                                 this.wait();
  19.                         } catch (InterruptedException e) {
  20.                                 e.printStackTrace();
  21.                         }
  22.                 }//写错位置了。。
  23.                 j--;
  24.                 System.out.println(Thread.currentThread().getName() + "-dec:" + j);
  25.                 this.notify();
  26.         }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马