黑马程序员技术交流社区
标题:
已解决多线程的锁
[打印本页]
作者:
毛标
时间:
2012-8-18 09:53
标题:
已解决多线程的锁
本帖最后由 毛标 于 2012-8-18 20:01 编辑
在学习多线程中,在锁的使用问题上总是弄不清什么时候用什么样的锁最合适,希望能有个详细的解答最好带着实例解释
作者:
左建飞
时间:
2012-8-18 11:10
我觉得在使用synchronized代码块或函数中,只有一个锁的情况下,使用this锁就是最好的。
<span style="font-size:12px;">public class InputOutputDemo2
{
public static void main(String[] args)
{
Res1 r=new Res1();
Thread t1=new Thread(new Input1(r));
Thread t2=new Thread(new Output1(r));
t1.start();
t2.start();
}
}
class Res1
{
private String name;
private String sex;
private boolean flag=false;
public synchronized void set(String name,String sex)//set和out是同步的,只需要设置一个锁,那么用this就可以了。
{
if(flag)
try{this.wait();}catch(InterruptedException e){}
this.name=name;
this.sex=sex;
flag=true;
this.notify();
}
public synchronized void out()
{
if(!flag)
try{this.wait();}catch(InterruptedException e){}
System.out.println(name+"-----"+sex);
flag=false;
this.notify();
}
}
class Input1 implements Runnable
{
private Res1 r;
Input1(Res1 r)
{
this.r=r;
}
public void run()
{
int x=0;
while(true)
{
if(x==0)
r.set("zhangsan","man");
else
r.set("lisi","woman");
x=(x+1)%2;
}
}
}
class Output1 implements Runnable
{
private Res1 r;
Output1(Res1 r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
</span>
复制代码
但是有两个以上的锁时,我们就需要另外建立对象,将其做为锁了。
public class DeadThreadDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t1=new Thread(new Test(true));
Thread t2=new Thread(new Test(false));
t1.start();
t2.start();
}
}
class Test implements Runnable
{
private boolean flag;
Test(boolean flag)
{
this.flag=flag;
}
public void run()
{
if (flag)
{
while (true)
{
synchronized(MyLock.locka)// 1 1与2两个代码块不是同步的,我们就需要建立两个对象,成为两个不同的锁。
{
System.out.println("if locka");
synchronized(MyLock.lockb)
{
System.out.println("if lockb");
}
}
}
}
else
{
while(true)
{
synchronized(MyLock.lockb)// 2
{
System.out.println("else lockb");
synchronized(MyLock.locka)
{
System.out.println("else locka");
}
}
}
}
}
}
class MyLock
{
static Object locka=new Object();
static Object lockb=new Object();
}
复制代码
在线程间通讯的情况下,使用Lock更为方便更有针对性。
package day12;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProducerCustomerDemo2
{
public static void main(String[] args)
{
Resource1 r=new Resource1();
Producer1 p=new Producer1(r);
Customer1 c=new Customer1(r);
Thread t1=new Thread(p);
Thread t2=new Thread(p);
Thread t3=new Thread(c);
Thread t4=new Thread(c);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource1
{
private String name;
private int count;
private boolean flag=false;
private Lock lock=new ReentrantLock();//1.5版本新特性。相当与synchronized语句。
//private Condition condition=lock.newCondition();//Condition相当与监视器方法,由lock的newCondition()方法获取。
private Condition condition_pro=lock.newCondition();//每个lock可以建立多个condition,它们可以互相等待唤醒。
private Condition condition_cus=lock.newCondition();
public void set(String name)throws InterruptedException
{
lock.lock();//上锁
try
{
while(flag)
condition_pro.await();//由await()替换wait()方法
this.name=name+"----"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者。。。"+this.name);
flag=true;
condition_cus.signal();//由signal()替换notify(),由signalAll()替换notifyAll()
}
finally
{
lock.unlock();
}
}
public void out()throws InterruptedException
{
lock.lock();
try
{
while(!flag)
condition_cus.await();
System.out.println(Thread.currentThread().getName()+"...消费者。。。。。。。。"+this.name);
flag=false;
condition_pro.signal();
}
finally
{
lock.unlock();
}
}
}
class Producer1 implements Runnable
{
private Resource1 r;
Producer1(Resource1 r)
{
this.r=r;
}
public void run()
{
while(true)
{
try
{
r.set("商品");
}
catch(InterruptedException e){}
}
}
}
class Customer1 implements Runnable
{
private Resource1 r;
Customer1(Resource1 r)
{
this.r=r;
}
public void run()
{
while(true)
{
try
{
r.out();
}
catch(InterruptedException e){}
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2