【发布时间】:2015-04-12 15:06:16
【问题描述】:
类:
class decrypt implements Runnable {
String name = "decode";
String text;
Thread t;
boolean ok = true;
boolean done = false;
decrypt(String en) {
t = new Thread(this, name);
System.out.println("Thread " + t.getName() + " Started!");
this.text = en;
}
decrypt() {
t = new Thread(this, "Main");
}
void ok(){
this.ok=true;
}
synchronized public void run() {
try {
Random rand = new Random();
System.out.println("Enter password");
Scanner input = new Scanner(System.in);
String p=input.next();
String fs = text.replace(p, "").trim();
System.out.println("Decrypting in progress.....");
t.sleep(500);
System.out.println("Original form of '" + text + "' is :'" + fs + "'");
ok();
System.out.println("");
done=true;
}catch (Exception e){
System.out.println("I handled an error for you, don't worry!");
}
}
}
主要:
.......
decrypt mm=new decrypt();
String sd="";
int itmessss=0;
while (!sd.equals("0") ){
if(mm.ok) { // at first time true, then always false!!
mm.t = new Thread(new decrypt(sd));
System.out.println("Please wait...");
mm.t.start();
}
}
.......
为什么
void ok(){
this.ok=true;
}
这不会将mm.ok 设置为true,它一开始是真的然后总是假的!!!
我也试过这个:
System.out.println("Original form of '" + text + "' is :'" + fs + "'");
this.ok=true;
System.out.println("");
done=true;
我不知道为什么这不起作用,调用者(线程)总是将其读取为 False
任何帮助将不胜感激
【问题讨论】:
标签: java multithreading class variables