1

当我运行程序时,它不显示“HIIII”。我是新手(有点)所以请不要“讨厌”。我的 wait() 语句错了吗?或者我做错了什么?是 ArrayIndexOutOfBounds catch 子句吗?请帮忙!

[edit]哦,这是主要方法吗?它什么都不做? [edit]我知道等待和通知是错误的......请不要提及它。

//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();
    }
}
    }
}
4

4 回答 4

1

似乎有几个问题

1) if(tru = Boolean.parseBoolean(args[0])){ 

上面的语句是赋值而不是比较。使用 == 运算符。

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

于 2012-10-11T06:45:06.717 回答
0

>java none true只会打印Cheats are on。但是您的问题是关于打印的Hiii。不是吗?你已经在课堂上的JOptionPane对话中得到了这个console。如果不初始化它,你怎么能期望你的程序打印Hiii?另外,为什么您在一个文件中编写了两个公共类?当您调用waitnottify方法时,您也缺少该synchronized语句。因此,当您启动线程时console,无论如何mainThingy都会抛出。IllegalMonitorStateException那么实际上你想做什么?

于 2012-10-11T06:39:05.763 回答
0
  1. main的方法实际上并没有开始任何事情
  2. wait并且notify必须synchronized在同一个监视器/锁上
  3. 你的两个线程没有共享同一个监视器/锁
  4. if (game = on)inmainThingy是一个作业,而不是一个检查,它应该是if (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 Concurrency 很有趣,但如果你不小心对待它,它会咬你一口。

阅读Java 中的 Currency

于 2012-10-11T06:45:05.633 回答
0

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

由于您似乎是在学习 Java 的第一步,我建议您再读两本书:

于 2012-10-11T07:36:34.333 回答