2015-04-12 33 views
-2

類:THIS.VALUE不工作

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.oktrue,它首先是真的,然後總是假!

我也試過這樣:

System.out.println("Original form of '" + text + "' is :'" + fs + "'"); 
       this.ok=true; 
       System.out.println(""); 
       done=true; 

我不知道爲什麼,這並不工作,調用者(線程)總是把它讀成假

任何幫助,將不勝感激

回答

1

Mark ok as volatile。否則僅評估一次ok

您正在訪問兩個主題的ok

爲了volatile的解釋參見例如: http://tutorials.jenkov.com/java-concurrency/volatile.html

我剛剛看了一遍你的問題。從我猜你的代碼應該做的事情來看,你不需要兩個線程。 Decrypt類實現Runnable。你可以從你的Decrypt對象中創建一個線程。

這是我能想象你的代碼可能是這樣的:

import java.util.Random; 
import java.util.Scanner; 

class Decrypt implements Runnable { 
    private String text; 
    volatile boolean ok = true; 
    boolean done = false; 

    public boolean isOk() { 
     return this.ok; 
    } 

    synchronized public void run() { 
     try { 
      final Random rand = new Random(); 
      System.out.println("Enter password"); 
      final Scanner input = new Scanner(System.in); 
      final String p = input.next(); 
      final String fs = text.replace(p, "").trim(); 
      System.out.println("Decrypting in progress....."); 
      Thread.sleep(500); 
      System.out.println("Original form of '" + text + "' is :'" + fs + "'"); 
      ok = true; 
      System.out.println(""); 
      done = true; 
     } catch (Exception e) { 
      System.out.println("I handled an error for you, don't worry!"); 
     } 
    } 

    public static void main(String args[]) { 
     Decrypt mm = new Decrypt(); 
     String sd = ""; 
     while (!sd.equals("0")) { 
      if (mm.isOk()) { // ok is private to Decrypt class, thus access by method 
       final Thread t = new Thread(mm); // You only need one Thread 
       System.out.println("Please wait..."); 
       t.start(); 
      } 
     } 
    } 
} 
+0

請exaplain,我是新來的Java。我在哪裏做什麼? – 11111111188u9u98

+0

聲明你的變量ok爲volatile boolean ok = true ;.編譯器和VM以其他方式優化訪問權限。如果沒有聲明爲volatile,它將被「緩存」。 –

+0

請參閱我已編輯的文章中的鏈接。 –