你要查找有關併發編程更多的數據,我可以告訴你,現在的一些基礎知識,好了,不那麼這樣基本的,但我會盡我所能:
在這裏,你有一個監視器,它是一個抽象的概念,在簡歷中,監視器是一個 類與所有它的使用 法「syncronized」 作爲改性劑,它意味着, 只有 一個線程 可以訪問 方法 一次。因此, 在 顯示器 變量 你 要打印, 並告訴你,如果 變量 是modified.Finally, 可以 看到 最重要的事情「標誌」, 時, 「等待()」和「通知()」方法, 那些方法 停止線程,或「播放」 他們再次。
您在
的PRINTVALUE()方法在這裏問 ,如果您的變量被改變,如果變量was'nt變化,把THEAD與wait()方法來睡覺,而當其他
方法changeValue()被執行時,值被修改,和notify()方法被調用時,喚醒線程,因此,做這一切,你能保證三兩件事:
安全性:這意味着線程將完成你想要的操作 沒有死鎖:意味着被放置的線程睡覺,將來會清醒。 互斥體:意味着只有一個線程正在執行關鍵代碼,例如,操作。 「++」不是原子的,被細分爲更多的一個動作,創建一個局部變量,讀取var,sum和asign,所以,如果多於一個線程在遊戲中,則該值可能不是連續的,例如:
i = 0;
i ++;
output: 1;
output: 2;
output: 3;
output: 5;
output: 4;
output: 7;
可能發生,即使如此,因爲有一個以上的線程中執行,這將在未來的代碼發生。那麼,這是有幾個線程,或多或少
public class Monitor {
private int value = 0;
public static boolean valueHasChanged = false;
public synchronized int changeValue(int newValue){
this.value = newValue;
Monitor.valueHasChanged = true;
this.notify();
return this.value + 1;
}
public synchronized void printValue(){
while(!Monitor.valueHasChanged){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(this.value);
Monitor.valueHasChanged = false;
}
public static void main(String[] args) {
Monitor ac = new Monitor();
BClass t1 = new BClass(ac);
AClass t2 = new AClass(ac);
t1.start();
t2.start();
}
public int getValue() {
return this.value;
}
}
現在線程編程的方式:
public class AClass extends Thread{
private Monitor ac;
public AClass(Monitor ac) {
this.ac = ac;
}
@Override
public void run() {
while(true){
this.ac.printValue();
}
}
}
最後:
public class BClass extends Thread{
private Monitor ac;
public BClass(Monitor ac) {
this.ac = ac;
}
@Override
public void run() {
int v = 0;
while(true){
this.ac.changeValue(v);
v++; // this sum is not secure, if you want to print an
// ascending order, the code is diferent, I will show in
// above.
}
}
現在,如果你想要一個有序打印:
顯示器看起來像:
public class Monitor {
private int value = 0;
public boolean valueHasChanged = false;
private boolean hasPrint = true;
public synchronized void changeValue(int newValue) {
this.value = newValue;
this.valueHasChanged = true;
this.notify();
}
public synchronized void changeValuePlusOne() {
while (!hasPrint) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.value++;
this.valueHasChanged = true;
this.hasPrint = false;
this.notifyAll();
}
public synchronized void printValue() {
while (!this.valueHasChanged) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(this.value);
this.valueHasChanged = false;
this.hasPrint = true;
this.notifyAll();
}
public static void main(String[] args) {
Monitor ac = new Monitor();
BClass t1 = new BClass(ac);
AClass t2 = new AClass(ac);
t1.start();
t2.start();
}
public int getValue() {
return this.value;
}
}
而且主題:
public class BClass extends Thread{
private Monitor ac;
public BClass(Monitor ac) {
this.ac = ac;
}
@Override
public void run() {
while(true){
this.ac.changeValuePlusOne();
}
}
}
其他線程的外觀等於:
public class AClass extends Thread{
private Monitor ac;
public AClass(Monitor ac) {
this.ac = ac;
}
@Override
public void run() {
while(true){
this.ac.printValue();
}
}
}
當您更改變量你設置'engineService.getFlag()''到TRUE'?我沒有在你的代碼中看到它... – brso05
你是在正確的軌道上...而是承諾使用可調用的,未來... –