catch子句后的java wait()语句
当我运行程序时,它不会显示“HIIII” 我是个新手,所以请不要“恨”。 我的wait()语句错了吗?或者我做错了什么? 是ArrayIndexOutOfBounds条款吗?请帮忙catch子句后的java wait()语句,java,Java,当我运行程序时,它不会显示“HIIII” 我是个新手,所以请不要“恨”。 我的wait()语句错了吗?或者我做错了什么? 是ArrayIndexOutOfBounds条款吗?请帮忙 [编辑]哦,那么这是主要的方法吗??它什么都不做? [编辑]我知道等待和通知是错误的。。。请不要客气 //this is the whole class import javax.swing.*; import javax.swing.JOptionPane; public class none { static
[编辑]
哦,那么这是主要的方法吗??它什么都不做?
[编辑]
我知道等待和通知是错误的。。。请不要客气
//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)
inmainThingy
是一项作业,而不是一项检查,如果(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)
inmainThingy
是一项作业,而不是一项检查,如果(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并发包
- 我建议不要使用标准的wait()-notify()结构。有更好的方法:Java并发包
控制台
和MainThnigy
?当您在console
中调用wait
时,您使用的是唤醒等待线程的哪条语句?您的main
方法没有任何作用。它检查参数,但随后存在。我试图让线程执行此操作,因为我无法使主方法notify()。。。有人知道让线程等待的更好方法吗。。?在哪里我可以实际使用main方法…?我没有找到任何调用线程控制台和MainThingy
的代码。通过定义,它们将如何运行?“请不要恨”请拼写所有单词的字母,如“请”。这有助于读者,进而帮助他们喜欢作者+1用于发布短代码。您在哪里使用控制台
和MainThnigy
?当您在console
中调用wait
时,您使用的是唤醒等待线程的哪条语句?您的main
方法没有任何作用。它检查参数,但随后存在。我试图让线程执行此操作,因为我无法使主方法notify()。。。有人知道让线程等待的更好方法吗。。?在哪里我可以实际使用main方法…?我没有找到任何调用线程控制台和MainThingy
的代码。仅通过定义,它们将如何运行?我试图让mainThingy
方法通知console
方法,但我混淆了wait()和notify()。。。主机不启动控制台吗?如果不是对不起。。。但是我不知道。我试图让mainThingy
方法通知console
方法,但是我与wait()和notify()混淆了。。。主机不启动控制台吗?如果不是对不起。。。但我没有我