Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中,如何在方法返回值之前停止执行_Java_Multithreading - Fatal编程技术网

在java中,如何在方法返回值之前停止执行

在java中,如何在方法返回值之前停止执行,java,multithreading,Java,Multithreading,我几乎是java线程的新手。我有一个场景,我在rabbitmq队列中发布JSON消息,一个外部服务正在对接收到的JSON执行操作,然后在执行外部服务后,它将在integer中返回一个值,指示执行是否成功 我想调用外部服务,然后等待返回值,即使生产者停止执行,直到消费者函数返回值 你的帮助非常值得赞赏。只要给我一个主题,比如是使用同步方法,还是未来的和可调用的接口等等 谢谢。请不要说“向我们展示你迄今为止所做的尝试”,我只需要你对如何做的建议。:) 看看我不久前试过的一个典型的生产者-消费者问题。

我几乎是
java线程
的新手。我有一个场景,我在
rabbitmq队列中发布
JSON
消息,一个外部服务正在对接收到的
JSON
执行操作,然后在执行外部服务后,它将在
integer
中返回一个值,指示执行是否成功

我想调用外部服务,然后等待返回值,即使生产者停止执行,直到消费者函数返回值

你的帮助非常值得赞赏。只要给我一个主题,比如是使用同步方法,还是未来的和可调用的接口等等


谢谢。请不要说“向我们展示你迄今为止所做的尝试”,我只需要你对如何做的建议。:)

看看我不久前试过的一个典型的生产者-消费者问题。。。没有原始博客/教程的链接,但代码如下:

 public class ProducerConsumerTest {
  public static void main(String[] args) {
    CubbyHole c = new CubbyHole();
    Producer p1 = new Producer(c, 1);
    Consumer c1 = new Consumer(c, 1);
    p1.start(); 
    c1.start();
  }
}
class CubbyHole {
 private int contents;
 private boolean available = false;
 public synchronized int get() {
   while (available == false) {
      try {
        wait();
       }
       catch (InterruptedException e) {
       }
    }
    available = false;
    notifyAll();
    return contents;
 }
  public synchronized void put(int value) {
    while (available == true) {
     try {
        wait();
     }
     catch (InterruptedException e) { 
     } 
    }
    contents = value;
    available = true;
    notifyAll();
 }
}

class Consumer extends Thread {
   private CubbyHole cubbyhole;
   private int number;
   public Consumer(CubbyHole c, int number) {
    cubbyhole = c;
   this.number = number;
   }
  public void run() {
     int value = 0;
     for (int i = 0; i < 10; i++) {
        value = cubbyhole.get();
          System.out.println("Consumer #" + this.number+ " got: " + value);
   }
  }
  }

  class Producer extends Thread {
  private CubbyHole cubbyhole;
  private int number;

 public Producer(CubbyHole c, int number) {
 cubbyhole = c;
 this.number = number;
 }

 public void run() {
 for (int i = 0; i < 10; i++) {
   cubbyhole.put(i);
   System.out.println("Producer #" + this.number+ " put: " + i);
    try {
    sleep((int)(Math.random() * 100));
     } catch (InterruptedException e) { }
  }
 }
 }
公共类ProducerConsumerTest{
公共静态void main(字符串[]args){
长方体孔c=新长方体孔();
生产者p1=新生产者(c,1);
消费者c1=新消费者(c,1);
p1.开始();
c1.开始();
}
}
类隔间{
私有int内容;
private boolean available=false;
公共同步int get(){
while(可用==false){
试一试{
等待();
}
捕捉(中断异常e){
}
}
可用=错误;
notifyAll();
返回内容;
}
公共同步的void put(int值){
while(可用==true){
试一试{
等待();
}
捕获(中断异常e){
} 
}
内容=价值;
可用=真实;
notifyAll();
}
}
类使用者扩展线程{
私人小房间;
私有整数;
公共消费者(储物间c,整数){
立方孔=c;
这个数字=数字;
}
公开募捐{
int值=0;
对于(int i=0;i<10;i++){
value=cubbyhole.get();
System.out.println(“Consumer#“+this.number+”got:“+value”);
}
}
}
类生成器扩展线程{
私人小房间;
私有整数;
公共制作人(小隔间c,整数){
立方孔=c;
这个数字=数字;
}
公开募捐{
对于(int i=0;i<10;i++){
小隔间。放置(i);
System.out.println(“Producer#“+this.number+”put:+i);
试一试{
睡眠((int)(Math.random()*100));
}捕获(中断异常e){}
}
}
}
诀窍是让生产者线程进入睡眠状态,直到消费者消费完之前的元素。在我提供的示例代码中,睡眠起到了关键作用

…通过一个好的旧while循环可以实现相同的效果。

在许多编程语言(包括Java)中,
join()
函数在名称和函数方面都非常常见。它所做的只是让调用线程等待被调用方/子线程完成,ei。它等待子线程返回

Thread t = new Thread() {
    public void run() {
        System.out.println("1");
        // Something that takes a long time to compute.
    }
 };
 t.start();
 t.join();
 System.out.println("2");
输出将是有序的。因为在t完成并返回之前,不会到达最后一行

Thread t = new Thread() {
    public void run() {
        System.out.println("1");
        // Something that takes a long time to compute.
    }
 };
 t.start();
 t.join();
 System.out.println("2");