黑马程序员技术交流社区
标题:
一个多线程问题
[打印本页]
作者:
柳 德 彬
时间:
2013-4-17 20:54
标题:
一个多线程问题
本帖最后由 柳 德 彬 于 2013-4-17 21:57 编辑
一个增加的线程,一个减少的线程,启动四个线程实现加1减1 ,,求解,,最后什么也没有输出,,JVM一直在运行。。。。。???
//增加的 Thread
public class Increase implements Runnable {
private TestThread2 obj;
public Increase(TestThread2 obj) {
this.obj = obj;
}
public void run() {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep((long) Math.random() * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
obj.inc();
}
}
}
复制代码
//减少的线程
public class Decrease implements Runnable{
private TestThread2 obj;
public Decrease(TestThread2 obj){
this.obj=obj;
}
public void run(){
for (int i = 0; i < 20; i++) {
try {
Thread.sleep((long)Math.random()*500);
} catch (InterruptedException e) {
e.printStackTrace();
}
obj.dec();
}
}
}
复制代码
//加1减1的两个方法
public class TestThread2 {
private int j;
public synchronized void inc() {
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) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
j--;
System.out.println(Thread.currentThread().getName() + "-dec:" + j);
notify();
}
}
}
复制代码
// 测试
public class MainClass {
public static void main(String[] args) {
TestThread2 t = new TestThread2();
Thread inc = new Thread(new Increase(t));
Thread dec = new Thread(new Decrease(t));
inc.start();
dec.start();
}
}
复制代码
作者:
黑马伍哲沂
时间:
2013-4-17 21:44
写了那么多没回复成功,我真心郁闷了。
你分别在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();
}
}
}
作者:
黑马伍哲沂
时间:
2013-4-17 21:44
写了那么多没回复成功,我真心郁闷了。
你分别在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();
}
}
}
作者:
柳 德 彬
时间:
2015-9-5 23:05
//自增++ 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(); }
作者:
柳 德 彬
时间:
2015-9-5 23:06
//自增++
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();
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2