Waiting for a thread

Hi,

i have a problem with threads...

in my "main" programm there is a method, that starts other methods...

In one method there are started two new threads, whitch copys the data from one DBMS to another. Now the main- programm should wait untill the threads have finished copying.

publicvoid steuerung(){

filedescription();

while (erzTab||erzDaten||erzInd||erzView){

if (erzTab)

createTable();

elseif (erzDaten&&alleDaten&&direkt)

datenAllDirekt();//==>starts two threads

//now i want to wait.....

elseif (erzDaten&&!alleDaten&&direkt)

datenTeilDirekt();

elseif (erzDaten&&!direkt)

datenDatei();

elseif (erzInd)

createIndex();

elseif (erzView)

createView();

}

return;

}

The method datenAllDirekt looks like following...

publicvoid datenAllDirekt(){

dataRead reader =new dataRead (DBread,con_as400,logfile,tempPfad);

reader.start();

daaWrite write =new dataWrite(DBwrite, con_MSsql, logfile, tempPfad, reader);

write.start();

I have insert the following code at "//now i want to wait"

try{

wait();

}catch (InterruptedException ex){

}

But then an "IllegalMonitorStateException: current thread not owner" occures.

How can I solve my problem?

Thanks for help....

[2427 byte] By [da_lua] at [2007-10-2 2:06:02]
# 1

Because you don't own this. By calling wait() you implicitely calling this.wait(), but you don't synchronized on this. Either synchronize on this or synchronize on your object and call myObject.wait().

Instead of using wait() you can also insert an endless loop that executes sleep() and checks for a flag to raise (e.g. implement a listener).

MartinHilperta at 2007-7-15 19:47:28 > top of Java-index,Other Topics,Algorithms...
# 2

It seems to be working with

public synchronized void datenAllDirekt() {

dataRead reader = new dataRead (DBread,con_as400,logfile,tempPfad);

reader.start();

daaWrite write = new dataWrite(DBwrite, con_MSsql, logfile, tempPfad, reader);

write.start();

}

... i'll see later if not... :-)

da_lua at 2007-7-15 19:47:28 > top of Java-index,Other Topics,Algorithms...
# 3

there was missing something.... :-)

public synchronized void datenAllDirekt() {

dataRead reader = new dataRead (DBread,con_as400,logfile,tempPfad);

reader.start();

daaWrite write = new dataWrite(DBwrite, con_MSsql, logfile, tempPfad, reader);

write.start();

while(erzDaten){

try {

this.wait();

} catch (InterruptedException ex) {

}

}

}

da_lua at 2007-7-15 19:47:28 > top of Java-index,Other Topics,Algorithms...
# 4

dataRead reader = new dataRead (DBread,con_as400,logfile,tempPfad);

reader.start();

daaWrite write = new dataWrite(DBwrite, con_MSsql, logfile, tempPfad, reader);

write.start();

// Wait until both have finished

reader.join();

write.join();

sabre150a at 2007-7-15 19:47:28 > top of Java-index,Other Topics,Algorithms...
# 5
IT WORKS!!!Thanks for help!!!
da_lua at 2007-7-15 19:47:28 > top of Java-index,Other Topics,Algorithms...