这一章节我们来讨论一下suspend缺点-独占。
1.return的失效
package com.ray.deepintothread.ch01.topic_11;
public class SuspendMonopolize_1 {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws InterruptedException {
ThreadFive threadFive = new ThreadFive();
Thread thread = new Thread(threadFive);
thread.start();
Thread.sleep(50);
thread.suspend();
Thread.sleep(50);
thread.resume();
return;
}
}
class ThreadFive implements Runnable {
private int count = 0;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public void run() {
while (true) {
System.out.println(count++);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
输出:
0
1
2
3
4
从输出可以看见,return已经失效了,我们通过debug得到,其实return一直都没有执行过,因此,失效,这是由于其中一个线程使用了suspend方法,线程被独占了,其他的线程进不去,从而也导致return无法起作用,因为锁住了
2.进一步说明由于独占,引起线程被锁,无法共享变量或方法
package com.ray.deepintothread.ch01.topic_11;
public class SuspendMonopolize_2 {
public static void main(String[] args) throws InterruptedException {
Test test = new Test();
ThreadOne threadOne = new ThreadOne(test);
Thread thread = new Thread(threadOne);
thread.start();
Thread.sleep(50);
Thread thread2 = new Thread(threadOne);
thread2.start();
}
}
class Test {
@SuppressWarnings("deprecation")
public synchronized void printStr() {
System.out.println(Thread.currentThread().getName() + " begin");
Thread.currentThread().suspend();
System.out.println(Thread.currentThread().getName() + " end");
}
}
class ThreadOne implements Runnable {
private Test test;
public ThreadOne(Test test) {
this.test = test;
}
@Override
public void run() {
test.printStr();
}
}
输出:
Thread-0 begin
程序是一直运行的,但是由于同步方法被某个线程独占,其他线程又进不去,形成死锁现象
3.进一步介绍有独占所引起的死锁
package com.ray.deepintothread.ch01.topic_11;
public class SuspendMonopolize_3 {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws InterruptedException {
ThreadTwo threadTwo = new ThreadTwo();
Thread thread = new Thread(threadTwo);
thread.start();
Thread.sleep(50);
thread.suspend();
System.out.println("main end");
}
}
class ThreadTwo implements Runnable {
private int count = 0;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public void run() {
while (true) {
count++;
System.out.println(count);
}
}
}
输出:
1
2
3
。。。
到某个数字,就停下来不输出了
程序是一直都在执行的,但是两个线程同时print,而print是同步方法,我们可以从源码看出:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
当两个线程进入同步方法,但是其中一个线程使用了suspend方法,独占方法,因此造成死锁现象
4.解决上面死锁的其中一个方法,使用我们之前的异常法或者是暴力stop法
package com.ray.deepintothread.ch01.topic_11;
public class SuspendMonopolize_4 {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws InterruptedException {
ThreadThree threadThree = new ThreadThree();
Thread thread = new Thread(threadThree);
thread.start();
Thread.sleep(50);
thread.suspend();
thread.stop();
System.out.println("main end");
}
}
class ThreadThree implements Runnable {
private int count = 0;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public void run() {
while (true) {
count++;
System.out.println(count);
}
}
}
输出:
1
2
3
。。。
数字
main end
package com.ray.deepintothread.ch01.topic_11;
public class SuspendMonopolize_5 {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws InterruptedException {
ThreadFour threadFour = new ThreadFour();
Thread thread = new Thread(threadFour);
thread.start();
Thread.sleep(50);
thread.suspend();
throw new RuntimeException();
}
}
class ThreadFour implements Runnable {
private int count = 0;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public void run() {
while (true) {
count++;
System.out.println(count);
}
}
}
输出:
1
2
。。。
1038
java.lang.RuntimeException
at com.ray.deepintothread.ch01.topic_11.SuspendMonopolize_5.main(SuspendMonopolize_5.java:11)
总结:这一章节主要介绍suspend和resume这两个方法的其中一个缺点-独占