Thread priorities?

There are two threads that have been started. Thread one is executing, when this thread encounters a certain line of code pause execution until the other thread completes execution, then finish execution of the thread that has been paused. Is there any way of doing this?

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can do this using the wait-notify mechanism (see Sun thread tutorial) but i'm skeptical you need this in your project...

eire_irelandAuthor Commented: 2003-12-09

Well I think I do, because both threads are calling the same function. The return value of the function depends on the second thread that has started. So I want the first thread to wait until the second thread has called the function before I call the function in the first thread.

Player1 sends "rock" and its name
Player2 sends "scissors" and its name

Having received two messages, server declares Player1 the winner

eire_irelandAuthor Commented: 2003-12-09

I did try something like that CEHZ but it didnt work, I think the join way might work but its not recognising my thread names. Im using setName to name the thread and nameofthread.join() but its throwing an error.

eire_ireland do you want the second thread to start simultaneously with the first thread or after the first thread has joined?

If you do t1.join() then the first thread will finish what it's doing and then you can start the second one. If you start both threads at the same time and you use join() you run the risk of one thread finishing before join is ever getting called so join() will simply return.

I have written a very quick and simpl example using wait and notify as suggestedoriginally by CEHJ. I adapted the Thread example of the java threa tutorial, maybe you will be able to modify it to obtain what you want. The programme simply runs two threads "Blue" and "Red" and at some point the Blue thread waits for the Red thread to finish. The Red thread does a dummy calculation of a variable (I call it "result") and then it sets it to the result of a multiplication. The Blue thread then goes on and access it and displays it on the screen.

It's not generally a good idea, if you can't get something simple working, to try something more complex in the hope that it'll fix things.

I see no reason to do anything other than wait until the two players have taken their turns and then inform them of the outcome. There is no reason for synchronization of threads in the way you imagine.

You do not need to take a whole lot from the code I posted, just the principle of it, how to make a thread wait while another one is executing. Just run my code (copy/paste it), see if it is something you need and then you can adjust the principles in your application.

In that case join() won't help (unless you handle clients a little differently).
You need to use wait()/notify() in your threads similiar to the example girionis has posted above. Where your client thread would do something like:

- read value from user
- wait
- send outcome

And in the server notify clients once values recieved from both users

eire_irelandAuthor Commented: 2003-12-09

Generally maybe, but this way ive output going to the second client informing of the outcome. returnval(a var in the code below - num in the previous post is returnval) is initially set to zero when the firsts client calls it. But this value may be changed by the second client which is why I want to call the below function in the first client after the second client executes. I can see no reason why this wont work if a method is put forward that does what im asking?

You client thread needs to do something like this (where server is a reference to Serverz instance that started the thread):

public void run()
{
// read input from user

server.setResult(answer);

String result = server.getResult();

// send result to server
}

For a single game your server then just needs to start the two threads:

a = new FirstClient(serve.accept());
b = new SecondClient(serve.accept());

public void setResult(String result)
{
// store results

if (input recieved from both clients)
{
// all input recieved, so notify waiting thread

notifyAll();
}
else
{
// have not recieved input from other palyer so wait until it comes in
wait();
}
}

eire_irelandAuthor Commented: 2003-12-09

I think this could work but its throwing an error:

try{

FirstClient a = new FirstClient(serve.accept());
SecondClient b = new SecondClient(serve.accept());

// wait for thread to finish

a.join();
b.join();
display.append("\n\n First Player to connect sent" +Choiseone);
display.append("\n\n Second Player to connect sent" +Choisetwo);

}
catch( IOException e )
{
e.printStackTrace() ;
}

C:\Documents and Settings\Adrian Ron\Desktop\Serverz.java:74: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
a.join();
^
C:\Documents and Settings\Adrian Ron\Desktop\Serverz.java:75: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
b.join();
^

> how am I going to send the result (based on the choice) to each client?

that was mentioned above. thayts why join() is not approriate with your current architecture. Either use wait()/notify() as I outlined above, or move the code that returns the results to the client out of your clients run() emthod into it's own.
I'd suggest the wait/notify approach.

eire_irelandAuthor Commented: 2003-12-09

output and socket streams are not closing. Output sends a string to the server, iand socket is just a socket connection. any other way to close them?

This is why, im closing sockets in a while loop, a silly mistake, if I take the closing methods out of the while loop a compile error saying the statements are not reachable is thrown. If I put the closing methods into a "finally" they dont get executed. What can I do?

Hence my suggestion to keep the clients in a List so you can send the result to the right client. Having said that, i seem to remember a long time back that i mentioned keeping them in a map, which would be more convenient:

Ive went in a different direction, its working at least, to stop one of the threads from executing i used a while loop waiting for the other thread to initialise a variable, when the variable got initialised the other thread had completed so the first could now complete.

I did try using a map but it seemed to go back to the same problem that I was trying to initially solve.
Using global variables is really what has gotten me out of this problem.