本节主要介绍线程的死锁。和如何终止线程
每个class也有一个锁,是这个class所对应的Class对象的锁。
wait、notify、notifyAll
1.每一个对象除了有一个锁之外,还有一个等待队列(wait set),当一个对象刚创建的时候,它的对待队列是空的。
2.我们应该在当前线程锁住对象的锁后,去调用该对象的wait方法。也就是说wait()和notify()需要在同步方法和同步块中。
3.当调用对象的notify方法时,将从该对象的等待队列中删除一个任意选择的线程,这个线程将再次成为可运行的线程。
4.当调用对象的notifyAll方法时,将从该对象的等待队列中删除所有等待的线程,这些线程将成为可运行的线程。
5.wait和notify主要用于producer-consumer这种关系中。
对于同步方法使用的是this对象的监视器wait方法和notify方法调用的是this对象的等待队列
生产者消费者示例:
package Test;
class Test {
public static void main(String[] args) {
Queue q = new Queue();
Produce p = new Produce(q);
Consumer c = new Consumer(q);
p.start();
c.start();
}
}
// 生产者线程类
class Produce extends Thread {
Queue q;
Produce(Queue q) {
this.q = q;
}
public void run() {
for (int i = 0; i < 10; i++) {
q.put(i);
System.out.println("Product i " + i);
}
}
}
// 消费者线程类
class Consumer extends Thread {
Queue q;
Consumer(Queue q) {
this.q = q;
}
public void run() {
while (true) {
System.out.println("Consumer get" + q.get());
}
}
}
class Queue {
int value;
// 判断队列是否是满的,如果是满的说明数据没有被消费者获取
boolean bFull = false;
public synchronized void put(int i) {
if (!bFull) {
value = i;
bFull = true;
notify();
}
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//就消费者而言
public synchronized int get() {
//如果队列有值则返回
if(!bFull)
{
try {
//消费者需要等待
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
bFull = false;
//通知生产者继续放置数据
notify();
return value;
}
}
结果
Consumer get0
Product i 0
Consumer get1
Product i 1
Consumer get2
Product i 2
Consumer get3
Product i 3
Consumer get4
Product i 4
Consumer get5
Product i 5
Consumer get6
Product i 6
Consumer get7
Product i 7
Consumer get8
Product i 8
Consumer get9
Product i 9
线程的终止
设置一个flag变量。
结合interrupt()方法
示例:
package ThreadStop;
class ThreadStopTest {
public static void main(String[] args)
{
Thread1 t = new Thread1();
t.start();
int index = 0;
while(true)
{
if(index++ == 500)
{
t.stopStop();
break;
}
System.out.println(Thread.currentThread().getName());
}
System.out.println("main exit");
}
}
class Thread1 extends Thread
{
private boolean isStop = false;
public synchronized void run()
{
while(!isStop)
{
try {
wait();
//进入到对象的等待队列需要有notify方法去唤醒
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
public void stopStop()
{
isStop = true;
}
}
结果:
main
main
main
main
main
main
main
main
main
main
main
main
main
main
main exit
。。。还在运行
当线程中调用wait方法 t处于等待状态,此时需要interrupt()
示例:
package ThreadStop;
class ThreadStopTest {
public static void main(String[] args)
{
Thread1 t = new Thread1();
t.start();
int index = 0;
while(true)
{
if(index++ == 500)
{
t.stopStop();
//终端t的线程,此时就会抛出异常
t.interrupt();
break;
}
System.out.println(Thread.currentThread().getName());
}
System.out.println("main exit");
}
}
class Thread1 extends Thread
{
private boolean isStop = false;
public synchronized void run()
{
while(!isStop)
{
try {
wait();
//进入到对象的等待队列需要有notify方法去唤醒
} catch (InterruptedException e) {
e.printStackTrace();
if(isStop)
return;
}
System.out.println(Thread.currentThread().getName());
}
}
public void stopStop()
{
isStop = true;
}
}