【Java并發(fā)】1.8暫停線程
暫停線程意味著此線程可以恢復運行伟众。在Java多線程中用suspend()方法暫停線程,使用resume()方法恢復線程的執(zhí)行颜曾。
如何使用suspend與resume方法巧婶?
public class MyThread extends Thread {
private long i = 1;
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(5000);
//A段
thread.suspend();
System.out.println("A="+System.currentTimeMillis()+",i="+thread.getI());
Thread.sleep(5000);
System.out.println("A="+System.currentTimeMillis()+",i="+thread.getI());
//B段
thread.resume();
Thread.sleep(5000);
//C段
thread.suspend();
System.out.println("B="+System.currentTimeMillis()+",i="+thread.getI());
Thread.sleep(5000);
System.out.println("B="+System.currentTimeMillis()+",i="+thread.getI());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public long getI() {
return i;
}
public void setI(long i) {
this.i = i;
}
@Override
public void run() {
super.run();
while (true) {
i++;
}
}
}
A=1513740411248,i=2343457221
A=1513740416249,i=2343457221
B=1513740421249,i=5066938112
B=1513740426249,i=5066938112
根據(jù)程序運行結(jié)果,發(fā)現(xiàn)線程可以被暫停以及恢復逊桦。
suspend與resume方法的缺點——獨占
public class SynchronizedObject {
public static void main(String[] args) {
try {
final SynchronizedObject object = new SynchronizedObject();
Thread threadA = new Thread() {
@Override
public void run() {
super.run();
object.printString();
}
};
threadA.setName("a");
threadA.start();
Thread.sleep(1000);
Thread threadB = new Thread() {
@Override
public void run() {
super.run();
System.out.println("threadB啟動了,但是進不了printString()方法抑进!只打印出一個begin");
System.out.println("因為printString()方法被a線程鎖定并且永久suspend暫停了强经!");
object.printString();
}
};
threadB.setName("b");
threadB.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized public void printString() {
System.out.println("begin.");
if (Thread.currentThread().getName().equals("a")) {
System.out.println("a線程被永久suspend了~");
Thread.currentThread().suspend();
}
System.out.println("end.");
}
}
begin.
a線程被永久suspend了~
threadB啟動了,但是進不了printString()方法寺渗!只打印出一個begin
因為printString()方法被a線程鎖定并且永久suspend暫停了匿情!
還有另一種獨占鎖的情況也需注意:
public class MyThread2 extends Thread{
private long i = 1;
public long getI() {
return i;
}
public void setI(long i) {
this.i = i;
}
@Override
public void run() {
super.run();
while (true) {
i++;
//System.out.println("i="+i);
}
}
public static void main(String[] args) {
try {
MyThread2 thread = new MyThread2();
thread.start();
Thread.sleep(1000);
thread.suspend();
System.out.println("main end");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
main end
程序運行結(jié)果為main end .
但是如果把System.out.println("i="+i); 注釋去掉兰迫,程序運行結(jié)果為:
......
i=183214
i=183215
i=183216
i=183217
i=183218
i=183219
i=183220
i=183221
此時main end 未輸出,是由于運行到println方法內(nèi)部時炬称,同步鎖未被釋放汁果。
println內(nèi)部源碼如下:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
這導致當前PringStream對象的println方法一直呈現(xiàn)“暫停”狀態(tài)玲躯,并且鎖未被釋放据德,而main方法中的代碼System.out.println("main end");遲遲不能打印。
雖然suspend()方法已過期作廢跷车,但是研究其過期的原因還是非常有意義的棘利。
suspend與resume缺點——不同步
在使用suspend與resume方法時也會出現(xiàn)線程暫停導致的數(shù)據(jù)不同步問題。
public class MyObject{
private String name = "name";
private String pwd = "pwd";
public void setValue(String name,String pwd){
this.name=name;
if(Thread.currentThread().getName().equals("a")){
System.out.println("停止a線程朽缴!");
Thread.currentThread().suspend();
}
this.pwd=pwd;
}
public void printString(){
System.out.println("name:" + name + ",pwd:" + pwd);
}
public static void main(String[] args) throws InterruptedException {
final MyObject object = new MyObject();
Thread thread1 = new Thread(){
@Override
public void run() {
super.run();
object.setValue("root","123456");
}
};
thread1.setName("a");
thread1.start();
Thread.sleep(1000);
Thread thread2 = new Thread(){
@Override
public void run() {
super.run();
object.printString();
}
};
thread2.start();javascript:void(null)
}
}
停止a線程善玫!
name:root,pwd:pwd
程序出現(xiàn)值不同步的情況。使用suspend需特別注意密强。
如何解決
未完待續(xù)...