当我运行程序时,它不会显示“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();
}
}
}
}
答案 0 :(得分:1)
似乎有几个问题
1) if(tru = Boolean.parseBoolean(args[0])){
以上声明是assignemt而非比较。使用==运算符。
2)应始终从同步块内部调用等待和通知。您的代码似乎没有这样做。
答案 1 :(得分:0)
>java none true
仅打印Cheats are on
。但你的问题是打印Hiii
。不是吗?你已经在JOptionPane
类的console
对话框中得到了它。如果没有初始化它,您如何期望您的程序打印Hiii
?为什么你在一个文件中写了两个公共类?当您调用wait
和nottify
方法时,您也错过了synchronized
语句。因此,当您开始线程console
和mainThingy
时,无论如何都会抛出IllegalMonitorStateException
。那么你到底想要做什么呢?
答案 2 :(得分:0)
main
方法实际上并未启动任何内容wait
和notify
必须synchronized
在同一台显示器/锁上if (game = on)
mainThingy
中的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();
}
}
}
}
}
}
醇>
使用示例更新
{{1}}
Java并发很有趣,但是如果你不小心并且很好地对待它,它会咬你。
答案 3 :(得分:0)
我建议不要使用标准的wait() - notify()结构。有更好的方法:Java并发包。
正如您在学习Java的第一步中所做的那样,我建议另外两本书: