forked from Pamgo/demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
线程挂起Suspend以及继续执行Resume(已废除,不推荐使用),因为Suspend在导致线程暂停的同时,并不会释放任何锁资源
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/com/example/concurrency/st/SuspendAndResumeThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |