线程问题,请帮忙看下,谢谢!
程序代码:package 线程;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Reasource2
{
private String name;
private int count=1;
private boolean flage=false;
private Lock lock=new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_com = lock.newCondition();
public void set(String name) throws InterruptedException//生产
{
lock.lock();
try
{
while(flage==true)
condition_pro.wait();
this.name=name+"......."+count++;
// Thread.currentThread().setName("生产者");
System.out.println(Thread.currentThread().getName()+".....生产者。。。。。"+"....."+this.name);
flage=true;
condition_com.signal();
}
finally
{
lock.unlock();
}
}
public void out() throws InterruptedException//消费
{
lock.lock();
try
{
while(flage==false)
condition_com.wait();
// Thread.currentThread().setName("消费者");
System.out.println(Thread.currentThread().getName()+"...消费者"+"....."+this.name);
flage=false;
condition_pro.signal();
}
finally
{
lock.unlock();
}
}
}
class Producer2 implements Runnable//生产者
{
private Reasource2 r;
private int x=0;
Producer2(Reasource2 r)
{
this.r=r;
}
public void run()
{
while(true)
{
if(x==0)
try {
r.set("这个");
} catch (InterruptedException e) {
}
else
try {
r.set("那个");
} catch (InterruptedException e) {
}
x=(x+1)%2;
}
}
}
class Consumer2 implements Runnable//消费者
{
Reasource2 r;
public Consumer2(Reasource2 r)
{
this.r=r;
}
public void run()
{
while(true)
try {
r.out();
} catch (InterruptedException e) {
}
}
}
public class ProducerConsumerDemo2 {
public static void main(String[] args)
{
Reasource2 r=new Reasource2();
Producer2 pro=new Producer2(r);
Consumer2 con=new Consumer2(r);
Thread t1=new Thread(pro);
Thread t3=new Thread(pro);
Thread t2=new Thread(con);
Thread t4=new Thread(con);
t1.start();
t3.start();
t2.start();
t4.start();
}
}根据教学视频写的代码怎么回事








