Whye does my Thread.wait() method apply to the whole program

I have GUI application that I want control with buttons. when I press one button(start) I want to execute one methode that calculates some numbers and prints out the result. Since there are severeal result I want the method to wait after the first answer, and I when i press my other button(continue) I want the method to continue and show me the next answer.

My problem is when I press the start button and get to the wait() method the whole application just freeze. The whole app seems to wait. I have tested with wait(some miliseconds) and the case is same. The whole app waits for some seconds and then continues. The clue is that i dont want the whole app to wait, only the method that prints out.

Here is the thread class. The only one in the code.

I also have a class that extends JFrame to build up the GUI. In that class I have a inne actionlistener class where I listen when the buttons are clicked.

When the button start is clicked it calls a method that gets a user input and sends it to the testNumber method. When the method is called it should stopp after the wait(); When I then press my other button it should continue. The continue button has only a notify() method.

import java.io.*;

import javax.swing.*;

import javax.swing.filechooser.*;

import java.awt.*;

import java.awt.event.*;

import easyIO.*;

import java.util.*;

class Ruteextends Thread

{

Rute()

{

}

synchronizedvoid testNumber(int radNr,int kolNr)

{

int nesteRad = radNr;

int nesteKol = kolNr;

nesteKol++;

if (nesteKol == brettSt鴕relse)

{

nesteKol = 0;

nesteRad++;

}

if (brett[radNr][kolNr] == 0)

{

for (int n=1; n<=brettSt鴕relse; n++)

{

if (gyldigTall(n,radNr,kolNr,brett))

{

brett[radNr][kolNr] = n;//legger inn verdier i brettet.

if (sisteRute(radNr, kolNr))

{

tegnBrett(brett);

l鴖ninger++;

}

else

{

testAlleSiffer(nesteRad, nesteKol);

}

brett[radNr][kolNr] = 0;

}

}

}

else

{

if (something)

{

System.out.println("Wait after this ");

try

{

//Thread.sleep(1*1000);

wait();//When it comes here the whole app waits

notify();

}

catch(InterruptedException e)

{

System.out.println("Du fikk en wait feil: " + e);

}

}

else

{

doSomethingElse();

}

}

}

[4324 byte] By [PhpDudea] at [2007-11-27 1:06:31]
# 1

synchronized is your problem. it locks everything else.

i think you would better do sth like this:

class Rute implements Runnable

{

Rute()

{

Thread thread = new Thread().

thread.start();

}

and then put your code into the

run(){

if(this.button_not_pressed){

do what you want;

}else{

thread.wait(10);

}

}

Message was edited by:

anti43

anti43a at 2007-7-11 23:41:42 > top of Java-index,Java Essentials,Java Programming...
# 2
Ok, but when I do it without the synchronized i get a nullpointer: java.lang.IllegalMonitorStateException: current thread not owner on the wait() method.I dont want to wait to wait(10). I want it to wait while i press the other button
PhpDudea at 2007-7-11 23:41:42 > top of Java-index,Java Essentials,Java Programming...
# 3

sure you want to wait while you PRESS the other button? then

use mousePressed and mouseReleased. otherwise try my solution. it will just check every 10ms if you have changed the variable, and if you change that with wahtever button, youre there

Message was edited by:

anti43

anti43a at 2007-7-11 23:41:42 > top of Java-index,Java Essentials,Java Programming...
# 4
Sorry I didn get that "What you told me"your suggestion resulted in a nullpointer that said current thread not owner. I tried it
PhpDudea at 2007-7-11 23:41:42 > top of Java-index,Java Essentials,Java Programming...
# 5

its the way you try to access the thread probably.

sorry, i forgot, use sleep()

run(){

while(true){

if(this.button_not_pressed){

do what you want;

}else{

//instead of wait();

try { Thread.currentThread().sleep(10);} catch (InterruptedException e) {}

}

}

}

}

anti43a at 2007-7-11 23:41:42 > top of Java-index,Java Essentials,Java Programming...
# 6

hmm I see

But the sleep() method takes "sleep time" which means that it cant sleep intil I wake it up. It sleeps for amount of time, wakes up and moves on.

In my case it is necessery for me to wake it up when I press the next button.

I need to do it some way like the with synchronized method, just not to make whole the porgram wait, only the prefered method.

PhpDudea at 2007-7-11 23:41:42 > top of Java-index,Java Essentials,Java Programming...
# 7
hm okay, why dont sleep a very small amount of time? otherwise, search for how to use synchronized, i dont know this well, good luck!
anti43a at 2007-7-11 23:41:42 > top of Java-index,Java Essentials,Java Programming...
# 8

I don't think you've really got the idea of Swing button handling. What you do is to attach an object to each button which implements ActionListener, and the actionPerformed method of that object is invoked when the button is pressed. If you're using a background thread then these actionPerformed methods manipulate that thread. These methods don't wait, but it's perfectly normal for them to wake sleeping threads, or start new ones.

All GUI activity is carried out on a special thread called the dispatcher thread, and that includes these actionPerformed methods.

You'll probably use a shared boolean variable to control the thread.

Your thread code (don't extend thread, create a Runnable and pass to Threa constructor might look something like:

public class test extends Runnable {

private boolean button1Clicked = false;

private boolean button2 Clicked = false;

public sychronized void press1() {

button1Clicked = true;

notifiyAll();

}

public synchronized void press2() {

button2Clickeed = true;

notifyAll();

}

public void run() {

try {

sychronized(this) {

while(!button1Clicked)

wait():

}

// do stuff

sychronized(this) {

while(!button2Clicked)

wait():

}

//

} catch(InterruptedException() {

//

}

}

The two press methods being called from your actionPerformed methods.

malcolmmca at 2007-7-11 23:41:42 > top of Java-index,Java Essentials,Java Programming...