java 多线程一个小问题
importjava.util.concurrent.locks.*;classLockTest{publicstaticvoidmain(String[]args){C...
import java.util.concurrent.locks.*;
class LockTest{
public static void main(String[] args){
Commodity comm = new Commodity();
Thread t1 = new Thread(new Production(comm));
Thread t2 = new Thread(new Consumption(comm));
t1.start();
t2.start();
}
}
//商品
class Commodity{
private String name="";
int count;
Lock lock;
Condition condition_Pro;
Condition condition_Con;
Commodity(){
lock = new ReentrantLock();
condition_Pro = lock.newCondition();
condition_Con = lock.newCondition();
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}
//生产者
class Production implements Runnable{
Commodity com_Pro;
Production(Commodity comm){
this.com_Pro = comm;
}
public void run(){
com_Pro.lock.lock();
while(true){
com_Pro.setName("生产一个");
System.out.println(com_Pro.getName());
try {
com_Pro.condition_Con.signal();
com_Pro.condition_Pro.await();
} catch (InterruptedException e){
System.out.println("发生等待异常。");
} finally {
com_Pro.lock.unlock();
}
}
}
}
//消费者
class Consumption implements Runnable{
Commodity com_Con;
Consumption(Commodity comm){
this.com_Con = comm;
}
public void run(){
com_Con.lock.lock();//加锁
while(true){
com_Con.setName("消费一个");
System.out.println(com_Con.getName());
try{
com_Con.condition_Pro.signal();
com_Con.condition_Con.await();
} catch (InterruptedException e){
System.out.println("消费者发生了等待异常。");
} finally {
com_Con.lock.unlock();
}
}
}
}
--------------------------------
错哪里了?为啥会跑出
D:\java>java LockTest
生产一个
消费一个
生产一个
Exception in thread "Thread-0" java.lang.IllegalMon
at java.util.concurrent.locks.ReentrantLock
k.java:155)
at java.util.concurrent.locks.AbstractQueue
tQueuedSynchronizer.java:1260)
at java.util.concurrent.locks.ReentrantLock
0)
at Production.run(LockTest.java:49)
at java.lang.Thread.run(Thread.java:744) 展开
class LockTest{
public static void main(String[] args){
Commodity comm = new Commodity();
Thread t1 = new Thread(new Production(comm));
Thread t2 = new Thread(new Consumption(comm));
t1.start();
t2.start();
}
}
//商品
class Commodity{
private String name="";
int count;
Lock lock;
Condition condition_Pro;
Condition condition_Con;
Commodity(){
lock = new ReentrantLock();
condition_Pro = lock.newCondition();
condition_Con = lock.newCondition();
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}
//生产者
class Production implements Runnable{
Commodity com_Pro;
Production(Commodity comm){
this.com_Pro = comm;
}
public void run(){
com_Pro.lock.lock();
while(true){
com_Pro.setName("生产一个");
System.out.println(com_Pro.getName());
try {
com_Pro.condition_Con.signal();
com_Pro.condition_Pro.await();
} catch (InterruptedException e){
System.out.println("发生等待异常。");
} finally {
com_Pro.lock.unlock();
}
}
}
}
//消费者
class Consumption implements Runnable{
Commodity com_Con;
Consumption(Commodity comm){
this.com_Con = comm;
}
public void run(){
com_Con.lock.lock();//加锁
while(true){
com_Con.setName("消费一个");
System.out.println(com_Con.getName());
try{
com_Con.condition_Pro.signal();
com_Con.condition_Con.await();
} catch (InterruptedException e){
System.out.println("消费者发生了等待异常。");
} finally {
com_Con.lock.unlock();
}
}
}
}
--------------------------------
错哪里了?为啥会跑出
D:\java>java LockTest
生产一个
消费一个
生产一个
Exception in thread "Thread-0" java.lang.IllegalMon
at java.util.concurrent.locks.ReentrantLock
k.java:155)
at java.util.concurrent.locks.AbstractQueue
tQueuedSynchronizer.java:1260)
at java.util.concurrent.locks.ReentrantLock
0)
at Production.run(LockTest.java:49)
at java.lang.Thread.run(Thread.java:744) 展开
1个回答
展开全部
循环里加上
Thread.sleep(1);
class TestThreads extends Thread{
public void run(){
for(int i=0;i<=4;i++){
try {
System.out.println("test is operating!");
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class TestThreads2 extends Thread{
public void run(){
for(int i=0;i<=4;i++)
try {
System.out.println("test2 is operating!");
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class TestThread{
public static void main(String[] args) {
new TestThreads().start();
new TestThreads2().start();
}
Thread.sleep(1);
class TestThreads extends Thread{
public void run(){
for(int i=0;i<=4;i++){
try {
System.out.println("test is operating!");
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class TestThreads2 extends Thread{
public void run(){
for(int i=0;i<=4;i++)
try {
System.out.println("test2 is operating!");
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class TestThread{
public static void main(String[] args) {
new TestThreads().start();
new TestThreads2().start();
}
追问
我想的是你帮我分析上面的问题在那里?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询