你必须查找更多关于并发编程的数据,我现在可以告诉你一些基础知识,嗯,不是那么基础,但我会尽力而为:
在这里,你有一个监视器,它是一个抽象概念,在简历中,监视器是一个
类与它的一切
使用“同步”的方法
作为修饰语,它意味着,
只有
一根线
可以访问
方法
一次。所以,
在里面
监视器是
变量
你
想打印,
和“旗帜”,
告诉你如果
变量
被修改了。最后,
你可以
见
最重要的是,“wait()”和“notify()”方法,
那些方法
停止线程,或“播放”
又来了。
你问
在这里
printValue()方法,如果你的变量改变了,如果变量没有改变,用wait()方法让thead休眠,当其他的时候
执行changeValue()方法,修改值,调用notify()方法,唤醒线程,所以,做这一切,可以保证三件事:
安全:意味着线程会做你想做的事
没有死锁:意味着被置于睡眠状态的线程将来会被唤醒。
互斥:意味着只有一个线程在执行关键代码,例如操作。 “++”不是原子的,是在一个action里面细分,创建一个局部var,读取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();
}
}
}