對於同步
追其同步的根本的目的
在上個例子的基礎上
/**
* Java線程
*
* @author leizhimin
*/
public class Test {
public static void main(String[] args) {
User u = new User(
MyThread t
MyThread t
MyThread t
MyThread t
MyThread t
MyThread t
t
t
t
t
t
t
}
}
class MyThread extends Thread {
private User u;
private int y =
MyThread(String name
super(name);
this
this
}
public void run() {
u
}
}
class User {
private String code;
private int cash;
User(String code
de = code;
this
}
public String getCode() {
return code;
}
public void setCode(String code) {
de = code;
}
/**
* 業務方法
*
* @param x 添加x萬元
*/
public void oper(int x) {
try {
Thread
synchronized (this) {
this
System
}
Thread
} catch (InterruptedException e) {
e
}
}
@Override
public String toString() {
return
}
}
線程E運行結束
線程B運行結束
線程D運行結束
線程F運行結束
線程C運行結束
線程A運行結束
Process finished with exit code
注意
在使用synchronized關鍵字時候
同樣
本文出自
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25673.html