关于线程的几个方法研究
2019-07-11 本文已影响0人
最美下雨天
1、关于线程的终止
stop方法
public class ThreadTest {
/**
* 关于线程的几个方法的演示
* 线程中断:interrupt、stop
* join、yield、wait
* @param args
*/
static int sum=0;
static Object object=new Object();
static boolean isRun=true;
public static void main(String args[])
{
Thread consumer=new Thread(new Runnable() {
@Override
public void run() {
while (isRun)
{
synchronized (object)
{
if(sum>0)
{
sum--;
System.out.println("消费者线程进入============运行状态。。。"+sum);
//不要直接写notify
object.notifyAll();
}
else
{
try {
System.out.println("消费者线程进入等待状态。。。");
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
consumer.start();
Thread product=new Thread(new Runnable() {
@Override
public void run() {
while (isRun)
{
synchronized (object)
{
if(sum>=10)
{
try {
System.out.println("生产者线程进入等待状态。。。");
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else
{
sum++;
System.out.println("生产者线程进入============运行状态。。。"+sum);
object.notifyAll();
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
product.start();
}
}
看运行结果:
我们规定商品的数量不可能超过10,否则会产生严重后果
image.png
生产者消费者有条不紊的进行着
但是,如果我们随意使用了stop方法,我们在最后面加上stop方法
public class ThreadTest {
/**
* 关于线程的几个方法的演示
* 线程中断:interrupt、stop
* join、yield、wait
* @param args
*/
static int sum=0;
static Object object=new Object();
static boolean isRun=true;
public static void main(String args[])
{
Thread consumer=new Thread(new Runnable() {
@Override
public void run() {
while (isRun)
{
synchronized (object)
{
if(sum>0)
{
sum--;
System.out.println("消费者线程进入============运行状态。。。"+sum);
//不要直接写notify
object.notifyAll();
}
else
{
try {
System.out.println("消费者线程进入等待状态。。。");
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
consumer.start();
Thread product=new Thread(new Runnable() {
@Override
public void run() {
while (isRun)
{
synchronized (object)
{
if(sum>=10)
{
try {
System.out.println("生产者线程进入等待状态。。。");
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else
{
sum++;
System.out.println("生产者线程进入============运行状态。。。"+sum);
object.notifyAll();
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
product.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//终止生产者线程
product.stop();
}
}
运行:
image.png
生产者stop了,消费者进入了永远的阻塞状态
根据stop方法的注释
This method is inherently unsafe. Stopping a thread with
* Thread.stop causes it to unlock all of the monitors that it
* has locked
就是说这个方法是不安全的,调用stop方法后,会释放它持有的锁
,而且在调用stop方法的时候,线程正在执行哪块代码是不确定的,所以有了替代方法interrupt
//终止生产者线程
//注意特殊情况:
//1.执行interrupt的时候product线程正在sleep或者正在wait,那么product线程会抛出一个InterruptedException异常,并且clear中断状态
//此时如果用isInterrupted()检测,那么返回的还是false
product.interrupt();
public class ThreadTest {
/**
* 关于线程的几个方法的演示
* 线程中断:interrupt、stop
* join、yield、wait
* @param args
*/
static int sum=0;
static Object object=new Object();
static boolean isRun=true;
public static void main(String args[])
{
Thread consumer=new Thread(new Runnable() {
@Override
public void run() {
while (isRun)
{
synchronized (object)
{
if(sum>0)
{
sum--;
System.out.println("消费者线程进入============运行状态。。。"+sum);
//不要直接写notify
object.notifyAll();
}
else
{
try {
System.out.println("消费者线程进入等待状态。。。");
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
consumer.start();
Thread product=new Thread(new Runnable() {
@Override
public void run() {
while (isRun)
{
if(Thread.currentThread().isInterrupted())
{
System.out.println("----------------");
//线程处于终端状态了
isRun=false;
//清理资源
}
synchronized (object)
{
if(sum>=10)
{
try {
System.out.println("生产者线程进入等待状态。。。");
object.wait();
} catch (InterruptedException e) {
isRun=false;
e.printStackTrace();
}
}
else
{
sum++;
System.out.println("生产者线程进入============运行状态。。。"+sum);
object.notifyAll();
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
isRun=false;
e.printStackTrace();
}
}
}
});
product.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//终止生产者线程
//注意特殊情况:
//1.执行interrupt的时候product线程正在sleep或者正在wait,那么product线程会抛出一个InterruptedException异常,并且clear中断状态
//此时如果用isInterrupted()检测,那么返回的还是false
product.interrupt();
}
}
运行:
image.png