java线程夯住了怎么解决,不明白线程为什么卡住了? 求大神解救
該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓
不明白線程為什么卡住了? 求大神解救
代碼如下
package practice;
class Resour {
private String name;
private int count = 1;
boolean flag = false;
public synchronized void set(String name) {
while (flag)
try {
this.wait();
} catch (Exception e) {
}
this.name = name + count++;
System.out.println(Thread.currentThread().getName()
+ "...producer..." + this.name);
// this.sex = sex;
flag = true;
this.notifyAll();
}
public synchronized void out() {
while (!flag)
try {
this.wait();
} catch (Exception e) {
}
System.out.println(Thread.currentThread().getName()
+ "..。。.consumer.。。.." + this.name);
flag = false;
this.notifyAll();
}
}
class Producer implements Runnable {
private Resour r;
Producer(Resour r) {
this.r = r;
} // 以便對(duì)象的唯一性
public void run() {
while (true) {
r.set("+goods+");
}
}
}
class Consumer implements Runnable {
private Resour r;
Consumer(Resour r) {
this.r = r;
} // 以便對(duì)象的唯一性
public void run() {
r.out();
}
}
public class ProducerConsumerDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Resour r = new Resour();
Producer pro = new Producer(r);
Consumer con= new Consumer(r);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
總結(jié)
以上是生活随笔為你收集整理的java线程夯住了怎么解决,不明白线程为什么卡住了? 求大神解救的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 怎样解决VS2003无法调试的问题
- 下一篇: java tomcat原理图,浅谈tom