黑马程序员技术交流社区
标题:
毕老师多线程LOCK锁中的问题
[打印本页]
作者:
支天歌
时间:
2014-1-9 14:39
标题:
毕老师多线程LOCK锁中的问题
照着视频敲了好几遍,翻来覆去的找还是没有找到问题所在,每次输出都是打印一两次就停止了。
代码如下:
package 多线程;
import java.util.concurrent.locks.*;
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
//重点变化,使用Lock代替线程同步
private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();
public synchronized void set(String name)throws InterruptedException
{
lock.lock();
//全部唤醒,通过循环控制
try
{
while(flag)
condition_pro.await();
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"---生产者---"+this.name);
flag = true;
condition_con.signal();
}
finally
{
lock.unlock();
}
}
public synchronized void out() throws InterruptedException
{
lock.lock();
//全部唤醒,通过循环控制
try
{
while(!flag)
condition_con.await();
System.out.println(Thread.currentThread().getName()+"---消费者---"+this.name);
flag = false;
condition_pro.signal();
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable
{
private Resource res;
Producer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try {
res.set("-商品-");
} catch (InterruptedException e) {
//e.printStackTrace();
}
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try {
res.out();
} catch (InterruptedException e) {
//e.printStackTrace();
}
}
}
}
public class Example9 {
public static void main(String[] args) {
Resource r = new Resource();
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();
}
}
复制代码
作者:
贺奕凯
时间:
2014-1-9 14:55
去掉synchronized
import java.util.concurrent.locks.*;
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
//重点变化,使用Lock代替线程同步
private Lock lock = new ReentrantLock();
//通过已有的锁获取两组监视器,一组监视生产者,一组监视消费者。
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();
public void set(String name)throws InterruptedException
{
lock.lock();
//全部唤醒,通过循环控制
try
{
while(flag)
condition_pro.await();
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"---生产者---"+this.name);
flag = true;
condition_con.signal();
}
finally
{
lock.unlock();
}
}
public void out() throws InterruptedException
{
lock.lock();
//全部唤醒,通过循环控制
try
{
while(!flag)
condition_con.await();
System.out.println(Thread.currentThread().getName()+"---消费者---"+this.name);
flag = false;
condition_pro.signal();
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable
{
private Resource res;
Producer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try {
res.set("-商品-");
} catch (InterruptedException e) {
//e.printStackTrace();
}
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try {
res.out();
} catch (InterruptedException e) {
//e.printStackTrace();
}
}
}
}
public class Example9 {
public static void main(String[] args) {
Resource r = new Resource();
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();
}
}
复制代码
作者:
伍艳雄
时间:
2014-1-9 18:11
package 多线程;
import java.util.concurrent.locks.*;
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
//重点变化,使用Lock代替线程同步
private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();
public void set(String name)throws InterruptedException //把这里的synchronized 锁去掉.
{
lock.lock();
//全部唤醒,通过循环控制
try
{
while(flag)
condition_pro.await();
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"---生产者---"+this.name);
flag = true;
condition_con.signal();
}
finally
{
lock.unlock();
}
}
public void out() throws InterruptedException //这里的锁synchronized 也要去掉.
{
lock.lock();
//全部唤醒,通过循环控制
try
{
while(!flag)
condition_con.await();
System.out.println(Thread.currentThread().getName()+"---消费者---"+this.name);
flag = false;
condition_pro.signal();
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable
{
private Resource res;
Producer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try {
res.set("-商品-");
} catch (InterruptedException e) {
//e.printStackTrace();
}
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try {
res.out();
} catch (InterruptedException e) {
//e.printStackTrace();
}
}
}
}
public class Example9 {
public static void main(String[] args) {
Resource r = new Resource();
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();
}
}
复制代码
把16行,和41行的synchronized 去掉.
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2