Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
catch子句后的java wait()语句_Java - Fatal编程技术网

catch子句后的java wait()语句

catch子句后的java wait()语句,java,Java,当我运行程序时,它不会显示“HIIII” 我是个新手,所以请不要“恨”。 我的wait()语句错了吗?或者我做错了什么? 是ArrayIndexOutOfBounds条款吗?请帮忙 [编辑]哦,那么这是主要的方法吗??它什么都不做? [编辑]我知道等待和通知是错误的。。。请不要客气 //this is the whole class import javax.swing.*; import javax.swing.JOptionPane; public class none { static

当我运行程序时,它不会显示“HIIII” 我是个新手,所以请不要“恨”。 我的wait()语句错了吗?或者我做错了什么? 是ArrayIndexOutOfBounds条款吗?请帮忙

[编辑]
哦,那么这是主要的方法吗??它什么都不做?
[编辑]
我知道等待和通知是错误的。。。请不要客气

//this is the whole class
import javax.swing.*;
import javax.swing.JOptionPane;
public class none {

static boolean game;
final static boolean on = true;
final static boolean off = false;
static boolean cheatMode;
public static void main(String[] args) {
    game = on;
    boolean tru = true;
    try{
        if(tru = Boolean.parseBoolean(args[0])){
            cheatMode = on;
            System.out.println("Cheats are on.");
        }
        }
        catch(java.lang.ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
            System.out.println("Ignore this error, it's from not running it on the command prompt.");
        }
}



public class console extends Thread{
    public void run(){
        try{
        wait();
        JOptionPane.showMessageDialog(null,"HIIII");
        }
        catch(Exception e){
            e.printStackTrace();
            System.out.println("The console glitched...");
        }
//hiiii
        JOptionPane.showMessageDialog(null,"HIIII");
        }
    public class mainThingy extends Thread{
        public void run() {
        if(game = on)
        notify();
    }
}
    }
}

将只打印
作弊信息
。但是你的问题是关于打印Hiii的。不是吗?您已经在
控制台
类中的
作业窗格
对话框中找到了它。如果不初始化它,你怎么能期望你的程序打印出Hiii?。还有,为什么要在一个文件中编写两个公共类?当您调用
wait
nottify
方法时,您还缺少
synchronized
语句。因此,当您启动线程
console
mainThingy
时,这些线程无论如何都会抛出
IllegalMonitorStateException
。那么实际上你想做什么呢?

将只打印
上的作弊。但是你的问题是关于打印Hiii的。不是吗?您已经在
控制台
类中的
作业窗格
对话框中找到了它。如果不初始化它,你怎么能期望你的程序打印出Hiii?。还有,为什么要在一个文件中编写两个公共类?当您调用
wait
nottify
方法时,您还缺少
synchronized
语句。因此,当您启动线程
console
mainThingy
时,这些线程无论如何都会抛出
IllegalMonitorStateException
。那么实际上你想做什么


  • 您的
    main
    方法实际上并没有启动任何东西
  • 等待
    通知
    必须在同一监视器/锁上同步
  • 您的两个线程没有共享同一个监视器/锁
  • if(game=on)
    in
    mainThingy
    是一项作业,而不是一项检查,如果(game=on)
  • 使用示例更新

    public class TestThread {
    
        static boolean game;
        final static boolean on = true;
        final static boolean off = false;
        static boolean cheatMode;
    
        public static void main(String[] args) {
            game = on;
            boolean tru = true;
            try {
                if (args.length > 0) {
                    if (tru = Boolean.parseBoolean(args[0])) {
                        cheatMode = on;
                        System.out.println("Cheats are on.");
                    }
                }
            } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                e.printStackTrace();
                System.out.println("Ignore this error, it's from not running it on the command prompt.");
            }
    
            Console con = new Console();
            con.start();
    
            // Give time for the console thread to get started
            do {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(TestThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            } while (!con.isAlive());
    
            System.out.println("Start main...");
            Console.MainThingy main = new Console.MainThingy();
            main.start();
    
        }
    
        public static class Console extends Thread {
    
            // A shared lock that our two threads can communicate on...
            public static final Object WAIT_LOCK = new Object();
    
            public void run() {
                try {
                    System.out.println("Waiting...");
                    // Must "own" the monitor before we can call wait
                    synchronized (WAIT_LOCK) {
                        WAIT_LOCK.wait();
                    }
                    JOptionPane.showMessageDialog(null, "HIIII");
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("The console glitched...");
                }
            }
    
            public static class MainThingy extends Thread {
    
                public void run() {
                    if (game == on) {
                        // Must "own" the monitor before we can call notify
                        synchronized (WAIT_LOCK) {
                            System.out.println("Notify...");
                            WAIT_LOCK.notify();
                        }
                    }
                }
            }
        }
    }
    
    Java并发性很有趣,但如果您不小心使用它并善待它,它会咬到您

    通读


  • 您的
    main
    方法实际上并没有启动任何东西
  • 等待
    通知
    必须在同一监视器/锁上同步
  • 您的两个线程没有共享同一个监视器/锁
  • if(game=on)
    in
    mainThingy
    是一项作业,而不是一项检查,如果(game=on)
  • 使用示例更新

    public class TestThread {
    
        static boolean game;
        final static boolean on = true;
        final static boolean off = false;
        static boolean cheatMode;
    
        public static void main(String[] args) {
            game = on;
            boolean tru = true;
            try {
                if (args.length > 0) {
                    if (tru = Boolean.parseBoolean(args[0])) {
                        cheatMode = on;
                        System.out.println("Cheats are on.");
                    }
                }
            } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                e.printStackTrace();
                System.out.println("Ignore this error, it's from not running it on the command prompt.");
            }
    
            Console con = new Console();
            con.start();
    
            // Give time for the console thread to get started
            do {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(TestThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            } while (!con.isAlive());
    
            System.out.println("Start main...");
            Console.MainThingy main = new Console.MainThingy();
            main.start();
    
        }
    
        public static class Console extends Thread {
    
            // A shared lock that our two threads can communicate on...
            public static final Object WAIT_LOCK = new Object();
    
            public void run() {
                try {
                    System.out.println("Waiting...");
                    // Must "own" the monitor before we can call wait
                    synchronized (WAIT_LOCK) {
                        WAIT_LOCK.wait();
                    }
                    JOptionPane.showMessageDialog(null, "HIIII");
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("The console glitched...");
                }
            }
    
            public static class MainThingy extends Thread {
    
                public void run() {
                    if (game == on) {
                        // Must "own" the monitor before we can call notify
                        synchronized (WAIT_LOCK) {
                            System.out.println("Notify...");
                            WAIT_LOCK.notify();
                        }
                    }
                }
            }
        }
    }
    
    Java并发性很有趣,但如果您不小心使用它并善待它,它会咬到您


    通读一下

    似乎有两个问题

    1) if(tru = Boolean.parseBoolean(args[0])){ 
    
    上述声明是转让而非比较。使用==运算符


    2) Wait和notify应始终从同步块内部调用。您的代码似乎没有做到这一点。

    似乎存在两个问题

    1) if(tru = Boolean.parseBoolean(args[0])){ 
    
    上述声明是转让而非比较。使用==运算符


    2) Wait和notify应始终从同步块内部调用。您的代码似乎没有这样做。

    我建议不要使用标准的wait()-notify()结构。有更好的方法:Java并发包

    由于您似乎正处于学习Java的第一步,我推荐另外两本书:


      • 我建议不要使用标准的wait()-notify()结构。有更好的方法:Java并发包

        由于您似乎正处于学习Java的第一步,我推荐另外两本书:


        “请不要恨”请拼出像“请”这样的单词的所有字母。这有助于读者,进而帮助他们喜欢作者+1用于发布短代码。您在哪里使用
        控制台
        MainThnigy
        ?当您在
        console
        中调用
        wait
        时,您使用的是唤醒等待线程的哪条语句?您的
        main
        方法没有任何作用。它检查参数,但随后存在。我试图让线程执行此操作,因为我无法使主方法notify()。。。有人知道让线程等待的更好方法吗。。?在哪里我可以实际使用main方法…?我没有找到任何调用线程
        控制台
        MainThingy
        的代码。通过定义,它们将如何运行?“请不要恨”请拼写所有单词的字母,如“请”。这有助于读者,进而帮助他们喜欢作者+1用于发布短代码。您在哪里使用
        控制台
        MainThnigy
        ?当您在
        console
        中调用
        wait
        时,您使用的是唤醒等待线程的哪条语句?您的
        main
        方法没有任何作用。它检查参数,但随后存在。我试图让线程执行此操作,因为我无法使主方法notify()。。。有人知道让线程等待的更好方法吗。。?在哪里我可以实际使用main方法…?我没有找到任何调用线程
        控制台
        MainThingy
        的代码。仅通过定义,它们将如何运行?我试图让
        mainThingy
        方法通知
        console
        方法,但我混淆了wait()和notify()。。。主机不启动控制台吗?如果不是对不起。。。但是我不知道。我试图让
        mainThingy
        方法通知
        console
        方法,但是我与wait()和notify()混淆了。。。主机不启动控制台吗?如果不是对不起。。。但我没有我