this.value不起作用

时间:2015-04-12 15:06:16

标签: java multithreading class variables

类别:

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

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

ok标记为volatileok只会被评估一次。

您正在从两个主题访问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();
            }
        }
    }
}