Skip to content

Commit

Permalink
线程挂起Suspend以及继续执行Resume(已废除,不推荐使用),因为Suspend在导致线程暂停的同时,并不会释放任何锁资源
Browse files Browse the repository at this point in the history
  • Loading branch information
Pamgo committed Mar 4, 2018
1 parent 220fc1c commit 126527d
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.concurrency.st;

/**
* 线程挂起Suspend以及继续执行Resume(已废除,不推荐使用)
* @author OKali
* 因为Suspend在导致线程暂停的同时,并不会释放任何锁资源
*/
public class SuspendAndResumeThread {

public static Object lock = new Object();

static ChangeObjectThread thread1 = new ChangeObjectThread("T1");

static ChangeObjectThread thread2 = new ChangeObjectThread("T2");

public static class ChangeObjectThread extends Thread {

public ChangeObjectThread(String name) {
super.setName(name);
}

@Override
public void run() {
synchronized (lock) {
System.out.println("in " + getName());
Thread.currentThread().suspend();
}
}
}

public static void main(String[] args) throws InterruptedException {
thread1.start();
thread2.start();
Thread.sleep(100);
thread1.resume();
thread2.resume();
thread1.join();
thread2.join();
}
}

0 comments on commit 126527d

Please sign in to comment.