貌似线程死锁了,哪位兄弟路过帮忙看看!!!!!!!
这是一个关于线程的代码,好像死锁了,希望帮忙看看。--------------------编程问答-------------------- 你死循环里玩命的 lock.lock() 的 能不死锁吗 --------------------编程问答-------------------- 你在同一个线程中既condition.await();又condition.signal();
package com.itcast.thread;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class InputOutputDemo {
public static void main(String[] args) {
Resource res = new Resource(false);
Input in = new Input(res);
Output out = new Output(res);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}
class Output implements Runnable {
Resource res;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public Output(Resource res) {
this.res = res;
}
public void run() {
while (true) {
lock.lock();
try {
while (!res.getFlag()) {
try {
condition.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(res.getName() + "--------" + res.getSex());
res.setFlag(false);
condition.signal();
} catch (Exception e) {
// TODO: handle exception
}finally{
lock.unlock();
}
}
}
}
class Input implements Runnable {
Resource res;
// 创建锁
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public Input(Resource res) {
this.res = res;
}
boolean b = false;
public void run() {
while (true) {
lock.lock();
try {
// 进行判断,是否要进行等待
while (res.getFlag()) {
try {
condition.await(); //线程等待
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (b) {
res.setName("mike");
res.setSex("man");
b = false;
} else {
res.setName("晓晓");
res.setSex("女");
b = true;
}
res.setFlag(true);
condition.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
}
// 资源 类
class Resource {
private String name;
private String sex;
private boolean flag;
public Resource(boolean flag) {
this.setFlag(flag);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public boolean getFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
你的condition是实例变量,是属于某个线程独有的,当这个线程await调用了就休眠了,就不执行了,根本不会调用该线程里的condition的signal方法了,何来唤醒? --------------------编程问答-------------------- 能不能说的详细点,我是初学线程,对多线程的控制总是很迷糊。 --------------------编程问答-------------------- 我一开始的时候采用的是synchronized的同步代码的时候,是可以运行的,但是改成lock的时候就不行了 --------------------编程问答-------------------- 2个线程共享一个 ReentrantLock 就可以解决啦。await之后 锁释放,singl之后才能,获取锁。而你的signal
在锁里。你说呢 --------------------编程问答--------------------
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class InputOutputDemo {
public static void main(String[] args) {
Resource res = new Resource(false);
Input in = new Input(res);
Output out = new Output(res);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}
class Output implements Runnable {
Resource res;
public Output(Resource res) {
this.res = res;
}
public void run() {
while (true) {
res.lock.lock();
try {
while (!res.getFlag()) {
try {
res.condition1.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(res.getName() + "--------" + res.getSex());
res.setFlag(false);
res.condition2.signal();
} catch (Exception e) {
// TODO: handle exception
}finally{
res.lock.unlock();
}
}
}
}
class Input implements Runnable {
Resource res;
// 创建锁
public Input(Resource res) {
this.res = res;
}
boolean b = false;
public void run() {
while (true) {
res.lock.lock();
try {
// 进行判断,是否要进行等待
while (res.getFlag()) {
try {
res.condition2.await(); //线程等待
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (b) {
res.setName("mike");
res.setSex("man");
b = false;
} else {
res.setName("晓晓");
res.setSex("女");
b = true;
}
res.setFlag(true);
res.condition1.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
res.lock.unlock();
}
}
}
}
// 资源 类
class Resource {
Lock lock = new ReentrantLock();
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
private String name;
private String sex;
private boolean flag;
public Resource(boolean flag) {
this.setFlag(flag);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public boolean getFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
没怎么明白你想干什么 但是至少搞清楚你在两个线程在竞争什么? --------------------编程问答--------------------
补充:Java , Java EE