【问题标题】:this.value is not workingthis.value 不起作用
【发布时间】: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


    【解决方案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();
                }
            }
        }
    }
    

    【讨论】:

    • 请解释一下,我是 Java 新手。我必须在哪里做这些?
    • 将变量 ok 声明为 volatile boolean ok = true;。编译器和 VM 以其他方式优化对 ok 的访问。如果没有声明为 volatile,它会被“缓存”。
    • 请再次阅读我编辑的帖子。更详细的评论然后是“不起作用!”那会很有帮助。
    • 但是这一行:`final String fs = text.replace(p, "").trim();` 返回错误java.lang.NullPointerException at com.book.ch5.decrypt.run(Main.java:609) at java.lang.Thread.run(Thread.java:745) Else 没问题
    • 所以文本为空。你需要它吗?然后在启动线程之前设置它。我不知道你的代码应该做什么。我的代码是对你问的线程问题的猜测。
    猜你喜欢
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    相关资源
    最近更新 更多