java信号量的问题
我是java新手,我只是想通过下面的例子来感受一下这种语言。谁能告诉我为什么下面的程序只显示:
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");
Q.semp.release ();
}
catch (Exception e) {
System.out.println (e.getMessage());
}
}
}
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");
Q.semp.release ();
}
catch (Exception e) {
System.out.println (e.getMessage());
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的问题不在于信号量,而在于你的线程。您的 run 方法没有执行,因为您正在实例化 Thread 的新实例,该实例不知道您创建的类并运行这些实例,而不是对您创建的类执行任何操作。所以你的 run 方法永远不会被调用。
具体来说,像这样的行:
没有引用它们所包含的类。它们只是创建一个新的 Thread 对象,该对象只有默认的 run 方法,然后启动它。
此站点提供了线程如何运行的示例(通过扩展线程或通过实现 Runnable)。不过,您将必须重新构建一些代码才能使其正常工作。尽管简单地更改要读取的行可能会起作用,
但这是一个坏主意,因为您将在对象的构造函数仍在运行时将对象作为值传递。更好的想法是让 main 方法构造每个对象,然后使用它们来启动线程运行。
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:
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
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.
此外:
try-finally 块。所以无论怎样
碰巧你总是释放
信号量,否则会出现死锁
必然会发生。
Furthermore:
try-finally block. So that whatever
happens you always release the
semaphore, otherwise a deadlock is
bound to happen.
您需要执行
并
查看此处以获取进一步参考:
http://www.exampledepot.com/egs/java.lang/BasicThread.html
You need to do
and
see here for further reference:
http://www.exampledepot.com/egs/java.lang/BasicThread.html