I am new to java and I am just trying to get feel of this language with the following example.Can anyone tell why the following program only shows:
calling prod
calling cons
import java.util.concurrent.*;
public class Trial5 {
static public void main(String[] arg){
new Prod();
new Cons();
}
}
class Q {
static Semaphore semc = new Semaphore(0);
static Semaphore semp = new Semaphore(1);
static int q[];
}
class Cons implements Runnable{
Thread t;
Cons () {
System.out.println("calling cons");
Thread t = new Thread();
t.start();
}
public void run () {
System.out.println("Running semc");
try {
System.out.println ("Waiting for Data.Acquiring semc");
Q.semc.acquire ();
if(Q.q[0] != 0) {
System.out.println(Q.q[0]);
Q.q[0] = 0;
} else {
wait ();
}
System.out.println ("Releasing semc");
Q.semc.release ();
}
catch (Exception e) {
System.out.println (e.getMessage());
}
}
}
class Prod implements Runnable {
Thread t;
Prod () {
System.out.println ("calling prod");
t = new Thread ();
t.start ();
}
public void run() {
System.out.println ("running semp");
try {
System.out.println ("Waiting for Data.Acquiring semp");
Q.semp.acquire ();
if (Q.q[0] == 0) {
System.out.println ("setting value semp");
Q.q[0] = 10;
} else {
Thread.sleep(100);
}
System.out.println ("Releasing semp");
开发者_如何学Go Q.semp.release ();
}
catch (Exception e) {
System.out.println (e.getMessage());
}
}
}
Your problem isn't with Semaphore, it's with your threads. Your run method is not executing because you're instantiating new instances of Thread which have no idea about the classes you've created and running those rather than doing anything with the classes you've created. So your run methods are never getting called.
Specifically, the lines like this:
Thread t = new Thread();
t.start();
have no reference to the classes they are contained in. They just create a new Thread object which has only the default run method and then start it.
This site has examples of how Threads get run (either through extending Thread or by implementing Runnable). You're going to have to restructure your code some to get it to work, though. Although it might work to simply change the lines to read
Thread t = new Thread(this);
that's a bad idea since you'd be passing the object as a value while its constructor is still running. A better idea would be to have your main method construct each object and then use them to start the threads running.
Furthermore:
- Always use semaphores in a try-finally block. So that whatever happens you always release the semaphore, otherwise a deadlock is bound to happen.
- You're calling 'wait()' (which is is a method of the Runnable (inherited from Object) instance) but you can't because you don't own the lock. For more on this see Monitor
- Thread.sleep(100) actually throws an InterruptedException: catch it and re-interrupt the thread as the interrupted flag is cleared when InterruptedException is thrown. For more on this topic see for instance Dealing with InterruptedException
You need to do
t = new Prod();
and
t= new Cons();
see here for further reference: http://www.exampledepot.com/egs/java.lang/BasicThread.html
精彩评论