问的好宽泛啊,建议看书看视频更加清晰吧
1 用同步代码块可以解决这个问题,格式为:
synchronized(对象)
{
需要被同步的代码;
}
同步的好处:解决了线程的安全问题;
同步的弊端:相对降低了效率,因为同步外的线程都会判断同步锁。
同步的前提:同步中必须有多个线程,并使用同一个锁。- class Resource
- {
- private String name;
- private int count=1;
- private boolean flag=false;
- public synchronized void set(String name)
- {
- while(flag)
- try{wait();}catch(InterruptedException e){}
- this.name=name+count;
- count++;
- System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
- flag=true;
- notifyAll();
- }
- public synchronized void get()
- {
- while(!flag)
- try{wait();}catch(InterruptedException e){}
- System.out.println(Thread.currentThread().getName()+"...消费者......"+this.name);
- flag=false;
- notifyAll();
- }
- }
-
- class Producer implements Runnable
- {
- private Resource r;
- Producer(Resource r)
- {
- this.r=r;
- }
- public void run()
- {
- while (true)
- {
- r.set("烤鸭");
- }
- }
- }
- class Consumer implements Runnable
- {
- private Resource r;
- Consumer(Resource r)
- {
- this.r=r;
- }
- public void run()
- {
- while (true)
- {
- r.get();
- }
- }
- }
-
- class ProducerConsumerDemo
- {
- public static void main(String[] args)
- {
- Resource r=new Resource();
- Producer pro=new Producer(r);
- Consumer con=new Consumer(r);
- Thread t0=new Thread(pro);
- Thread t1=new Thread(con);
- Thread t2=new Thread(pro);
- Thread t3=new Thread(con);
- t0.start();
- t1.start();
- t2.start();
- t3.start();
- }
- }
复制代码 接口 Lock
同步代码块,对于锁的操作时隐式的
jdk1.5以后,将同步和锁封装成了对象,并将操作锁的方式定义到了该对象中,将隐式动作变成了显式动作。
Lock lock=new ReentrantLock();
lock.lock();//获取锁
code...;//throw Exception();
lock.unlock();//释放锁
try
{
lock.lock();//获取锁
}
finally
{
lock.unlock;//释放锁
}
Lock接口:它的出现替代了同步代码块或者同步函数。将同步的隐式锁操作变成了显式锁操作;
同时更为灵活,可以一个锁上加上多组监视器。
lock();获取锁
unlock();释放锁,通常需要定义到finally代码块中。
Condition接口:它的出现替代了Object中的wait、notify、notifyAll方法。
将这些监视器方法单独进行了封装,变成了Condition监视器对象,
可以和任意锁组合。
await();//等待
signal();//唤醒一个等待线程
signalAll();//唤醒所有等待线程- import java.util.concurrent.locks.*;
- class Resource
- {
- private String name;
- private int count=1;
- private boolean flag=false;
- //创建一个锁对象
- Lock lock=new ReentrantLock();
- //通过已有的锁,获取该锁上的监视器对象
- //Condition con=lock.newCondition();
- //通过已有的锁,获取两组监视器;一组监视生产者,一组监视消费者
- Condition producer_con=lock.newCondition();
- Condition consumer_con=lock.newCondition();
- public void set(String name)
- {
- lock.lock();
- try
- {
- while(flag)
- try{producer_con.await();}catch(InterruptedException e){}
- this.name=name+count;
- count++;
- System.out.println(Thread.currentThread().getName()+"...生产者5.0..."+this.name);
- flag=true;
- consumer_con.signal();
- }
- finally
- {
- lock.unlock();
- }
-
- }
- public void get()
- {
- lock.lock();
- try
- {
- while(!flag)
- try{consumer_con.await();}catch(InterruptedException e){}
- System.out.println(Thread.currentThread().getName()+"...消费者........."+this.name);
- flag=false;
- producer_con.signal();
- }
- finally
- {
- lock.unlock();
- }
-
- }
- }
-
- class Producer implements Runnable
- {
- private Resource r;
- Producer(Resource r)
- {
- this.r=r;
- }
- public void run()
- {
- while (true)
- {
- r.set("烤鸭");
- }
- }
- }
- class Consumer implements Runnable
- {
- private Resource r;
- Consumer(Resource r)
- {
- this.r=r;
- }
- public void run()
- {
- while (true)
- {
- r.get();
- }
- }
- }
-
- class ProducerConsumerDemo
- {
- public static void main(String[] args)
- {
- Resource r=new Resource();
- Producer pro=new Producer(r);
- Consumer con=new Consumer(r);
- Thread t0=new Thread(pro);
- Thread t1=new Thread(con);
- Thread t2=new Thread(pro);
- Thread t3=new Thread(con);
- t0.start();
- t1.start();
- t2.start();
- t3.start();
- }
- }
复制代码 |