------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
1. 线程间通讯:
其实就是多个线程在操作同一个资源,但是操作的动作不同。
2. wait(),notify(),notifyAll(),用来操作线程为什么定义在了Object类中?
1. 这些方法存在与同步中。
2. 使用这些方法时必须要标识所属的同步的锁。
3. 锁可以时任意对象,所以任意对象调用的方法一定定义Object类中。
3. wait(),sleep()有什么区别?
wait():释放资源,释放锁。
sleep():释放资源,不释放锁。
4. 线程间通讯:
其实就是多个线程在操作同一个资源
但是操作的动作不同
5. 例子:
class Res
{
private String name;
private String sex;
boolean flag = false;
public void synchronized set(String name,String sex)
{
if(flsg)
{
try{this.wait();}catch{Exception e}{}//线程等待
}
this.name = name;
this.sex = sex;
flag = true;
this.notify();//线程唤醒
}
public void out()
{
if(!flag)
{
try{this.wait();}catch{Exception e}{}
}
System.out.println(name+"......."+sex);
flag = false;
this.notify();
}
}
class Input implements Runnable
{
private Res r ;
Input(Res r)
{
this.r = r;
}
public void run()
{
int x = 0;
while(true)
{
if(x==0)
{
r.set( "mike","man");
}
else
{
r.set( "丽丽","女女女");
}
x = (x+1)%2;
}
}
}
class Output implements Runnable
{
private Res r ;
Output(Res r)
{
this.r = r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
class InputOutputDemo
{
public static void main(String[] args)
{
Res r = new Res();
new Thread (new Input(r)).start();
new Thread (new Output(r)).start();
/*Input in = new Input(r);
Output out = new Output(r);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();*/
}
}
6. 生产者和消费者
如果有多个生产者和多个消费者,那么同步机制应该应用while循环,而唤醒机制应用 notifyAll();否则会出现安全问题,如:不用while循环的话,则不会重新判断,而直接 往下执行,会出现生产多个,而消费 一个,或者生产一个,消费多个,因此眼红 while循环,让被唤醒的线程再一次判断标记:而如果用 notify(),只唤醒一个,可 能唤醒本方的,会出现全部等待,notifyAll(),则唤醒全部。
7. JDK1.5中提供了多线程升级解决方案。
将同步Synchronized替换成现实Lock操作,将Object中的wait,notify,notifyAll,替 换了Condition对象,该对象可以lock锁,进行获取,该实例中,实现了本方只唤醒对 方操作。
例子:
import java.util.concurrent.locks.*;
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
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)
{
}
}
}
}
class Consumer implements Runnable
{
private Resource res ;
Consumer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try
{
res.out();
}
catch (InterruptedException e)
{
}
}
}
}
class InputOutputDemo
{
public static void main(String[] args)
{
Resource r = new Resource();
new Thread (new Input(r)).start();
new Thread (new Output(r)).start();
/*Input in = new Input(r);
Output out = new Output(r);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();*/
}
}
8. 停止线程:
只有一种,run方法结束,开启多线程运行,运行代码通常是循环结构,只要控制 住循环,就可以让run方法结束,也就是线程结束。
1.定义循环结束标记
因为线程运行代码一般都是循环,只要控制了循环即可。
2.使用interrupt(中断)方法
该方法时结束线程的冻结状态,时线程回到运行状态中来。
3.特殊情况:
当线程处于了冻结状态,就不会读取到标记,那么线程就不会结束,当没有指 定的方式让冻结的线程回复到运行状态时,这是需要对冻结状态进行清除,强 制让线程回复到运行状态中来,这样就可以操作标记让线程结束。Thread 类 中提供该方法interrupt();
9. Join :
当A线程执行到了B线程的.join()方法时,A就会等待,等B线程都执行完,A才 会执行,join可以用来临时加入线程执行。
10 学习心得和体会
学会运用线程之间的通信,知道用在什么地方。
知道 wait(),sleep()区别
wait():释放资源,释放锁。
sleep():释放资源,不释放锁。
将同步Synchronized替换成现实Lock操作,将Object中的wait,notify,notifyAll, 替 换了Condition对象,该对象可以lock锁,进行获取,该实例中,实现了本方只唤 醒对方操作。
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------