java信号量的问题

发布于 2024-11-18 00:23:45 字数 1970 浏览 4 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一笔一画续写前缘 2024-11-25 00:23:46

你的问题不在于信号量,而在于你的线程。您的 run 方法没有执行,因为您正在实例化 Thread 的新实例,该实例不知道您创建的类并运行这些实例,而不是对您创建的类执行任何操作。所以你的 run 方法永远不会被调用。

具体来说,像这样的行:

Thread t = new Thread();
t.start();

没有引用它们所包含的类。它们只是创建一个新的 Thread 对象,该对象只有默认的 run 方法,然后启动它。

此站点提供了线程如何运行的示例(通过扩展线程或通过实现 Runnable)。不过,您将必须重新构建一些代码才能使其正常工作。尽管简单地更改要读取的行可能会起作用,

Thread t = new Thread(this);

但这是一个坏主意,因为您将在对象的构造函数仍在运行时将对象作为值传递。更好的想法是让 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:

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.

往日 2024-11-25 00:23:46

此外:

  1. 始终在
    try-finally 块。所以无论怎样
    碰巧你总是释放
    信号量,否则会出现死锁
    必然会发生。
  2. 您正在调用“wait()”(这是 Runnable(继承自 Object)实例的一个方法),但您不能调用,因为您不拥有锁。有关详细信息,请参阅 Monitor
  3. Thread.sleep(100) 实际上会抛出 InterruptedException:捕获它并重新中断线程,因为抛出 InterruptedException 时中断标志会被清除。有关此主题的更多信息,请参阅处理 InterruptedException

Furthermore:

  1. Always use semaphores in a
    try-finally block. So that whatever
    happens you always release the
    semaphore, otherwise a deadlock is
    bound to happen.
  2. 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
  3. 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
未蓝澄海的烟 2024-11-25 00:23:46

您需要执行

t = new Prod();

t= new Cons();

查看此处以获取进一步参考:
http://www.exampledepot.com/egs/java.lang/BasicThread.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文