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,我有1,2,3个线程,每个线程都有A,B,C部分按顺序运行 1A、2A、3A、1B、2B、3B、1C、2C、3C。因此,在1A完成其运行后,它将等待线程3的信号继续执行其节B,以此类推。我怎样才能做到这一点?我必须重复使用线程,因此无法从节中生成线程。这是一个: 这是一个工程师的工作: 如果1,2,3的顺序不重要,但是所有A,所有B,所有C的顺序都重要,那么, 使用2个倒计时闩锁。 你可以直接开始一个部分。当一节完成时,倒计时闩锁 在B部分开始之前,等待()倒计时闩锁 所以应该有2个倒计时锁存器

我有1,2,3个线程,每个线程都有A,B,C部分按顺序运行 1A、2A、3A、1B、2B、3B、1C、2C、3C。因此,在1A完成其运行后,它将等待线程3的信号继续执行其节B,以此类推。我怎样才能做到这一点?我必须重复使用线程,因此无法从节中生成线程。

这是一个:

这是一个工程师的工作:


如果1,2,3的顺序不重要,但是所有A,所有B,所有C的顺序都重要,那么, 使用2个倒计时闩锁。 你可以直接开始一个部分。当一节完成时,倒计时闩锁

在B部分开始之前,等待()倒计时闩锁

所以应该有2个倒计时锁存器来同步部分B和部分C

    public class Test extends Thread
{

  CountDownLatch cdlA;
  CountDownLatch cdlB;
  int threadId;

  public Test(CountDownLatch cdlA, CountDownLatch cdlB,int threadNumber)
  {
    this.cdlA = cdlA;
    this.cdlB = cdlB;
    threadId = threadNumber;
  }

  @Override
  public void run()
  {
    test();
  }

  private   void test()
  {
    try
    {
      System.out.println("section A" +threadId);
      cdlA.countDown();

      cdlA.await();

      System.out.println("section B" +threadId);
      cdlB.countDown();

      cdlB.await();

      System.out.println("section C" +threadId);
    }
    catch (InterruptedException e)
    {

    }

  }

  public static void main(String[] args) throws InterruptedException
  {
    CountDownLatch a = new CountDownLatch(3);
    CountDownLatch b = new CountDownLatch(3);

    final Test test1 = new Test(a, b,1);

    final Test test2 = new Test(a, b,2);

    final Test test3 = new Test(a, b,3);

    test1.start();
    test2.start();
    test3.start();
  }
}

如果1,2,3的顺序不重要,但是所有A,所有B,所有C的顺序都重要,那么, 使用2个倒计时闩锁。 你可以直接开始一个部分。当一节完成时,倒计时闩锁

在B部分开始之前,等待()倒计时闩锁

所以应该有2个倒计时锁存器来同步部分B和部分C

    public class Test extends Thread
{

  CountDownLatch cdlA;
  CountDownLatch cdlB;
  int threadId;

  public Test(CountDownLatch cdlA, CountDownLatch cdlB,int threadNumber)
  {
    this.cdlA = cdlA;
    this.cdlB = cdlB;
    threadId = threadNumber;
  }

  @Override
  public void run()
  {
    test();
  }

  private   void test()
  {
    try
    {
      System.out.println("section A" +threadId);
      cdlA.countDown();

      cdlA.await();

      System.out.println("section B" +threadId);
      cdlB.countDown();

      cdlB.await();

      System.out.println("section C" +threadId);
    }
    catch (InterruptedException e)
    {

    }

  }

  public static void main(String[] args) throws InterruptedException
  {
    CountDownLatch a = new CountDownLatch(3);
    CountDownLatch b = new CountDownLatch(3);

    final Test test1 = new Test(a, b,1);

    final Test test2 = new Test(a, b,2);

    final Test test3 = new Test(a, b,3);

    test1.start();
    test2.start();
    test3.start();
  }
}

如果您有一个知道该做什么的委托,那么您可以使用executor服务来控制执行。为每个线程创建一个executors服务,提交,然后使用Future.get等待执行完成

另一种方法是使用一种球

static public void task(int no){
    System.out.println("task " + no + " on " + Thread.currentThread().getName());
}

public static void main(String[] args) throws Exception{
    BlockingQueue<Object> permissions = new ArrayBlockingQueue<>(1);

    var A = new Thread(){
        BlockingQueue<Object> waiting = new ArrayBlockingQueue<>(1);
        public void run(){
            for(int i = 0; i<3; i++){
                Object ball = null;
                try{
                    ball = waiting.take();
                    task(i);
                } catch(InterruptedException e){
                    throw new RuntimeException(e);
                }finally{
                    permissions.offer(ball);
                }
            }
        }
    };

    var B = new Thread(){
        BlockingQueue<Object> waiting = new ArrayBlockingQueue<>(1);
        public void run(){
            for(int i = 0; i<3; i++){
                Object ball = null;
                try{
                    ball = waiting.take();
                    task(i);
                } catch(InterruptedException e){
                    throw new RuntimeException(e);
                }finally{
                    permissions.offer(ball);
                }
            }
        }
    };

    A.start();
    B.start();
    Object ball = new Object();
    A.waiting.offer(ball);
    ball = permissions.take();
    B.waiting.offer(ball);
    ball = permissions.take();
    A.waiting.offer(ball);
    ball = permissions.take();
    B.waiting.offer(ball);
    ball = permissions.take();
    A.waiting.offer(ball);
    ball = permissions.take();
    B.waiting.offer(ball);
    ball = permissions.take();
    System.out.println("finished");
静态公共无效任务(int-no){
System.out.println(“任务”+no+”位于“+Thread.currentThread().getName());
}
公共静态void main(字符串[]args)引发异常{
BlockingQueue权限=新建ArrayBlockingQueue(1);
var A=新线程(){
BlockingQueue waiting=新阵列BlockingQueue(1);
公开募捐{

对于(int i=0;i如果您有一个知道要做什么的委托,那么您可以使用执行器服务来控制执行。例如,为每个线程创建一个执行器服务,提交,然后使用Future.get等待执行完成

另一种方法是使用一种球

static public void task(int no){
    System.out.println("task " + no + " on " + Thread.currentThread().getName());
}

public static void main(String[] args) throws Exception{
    BlockingQueue<Object> permissions = new ArrayBlockingQueue<>(1);

    var A = new Thread(){
        BlockingQueue<Object> waiting = new ArrayBlockingQueue<>(1);
        public void run(){
            for(int i = 0; i<3; i++){
                Object ball = null;
                try{
                    ball = waiting.take();
                    task(i);
                } catch(InterruptedException e){
                    throw new RuntimeException(e);
                }finally{
                    permissions.offer(ball);
                }
            }
        }
    };

    var B = new Thread(){
        BlockingQueue<Object> waiting = new ArrayBlockingQueue<>(1);
        public void run(){
            for(int i = 0; i<3; i++){
                Object ball = null;
                try{
                    ball = waiting.take();
                    task(i);
                } catch(InterruptedException e){
                    throw new RuntimeException(e);
                }finally{
                    permissions.offer(ball);
                }
            }
        }
    };

    A.start();
    B.start();
    Object ball = new Object();
    A.waiting.offer(ball);
    ball = permissions.take();
    B.waiting.offer(ball);
    ball = permissions.take();
    A.waiting.offer(ball);
    ball = permissions.take();
    B.waiting.offer(ball);
    ball = permissions.take();
    A.waiting.offer(ball);
    ball = permissions.take();
    B.waiting.offer(ball);
    ball = permissions.take();
    System.out.println("finished");
静态公共无效任务(int-no){
System.out.println(“任务”+no+”位于“+Thread.currentThread().getName());
}
公共静态void main(字符串[]args)引发异常{
BlockingQueue权限=新建ArrayBlockingQueue(1);
var A=新线程(){
BlockingQueue waiting=新阵列BlockingQueue(1);
公开募捐{

对于(int i=0;i这是我对这个问题的解决方案。我认为它背后的想法接近马特的解决方案。谢谢你们

 @Test
void test() throws InterruptedException {
    ExecutorService executor = Executors.newFixedThreadPool(3);
    SynchronousQueue<Integer> chromeQueue = new SynchronousQueue<>();
    SynchronousQueue<Integer> firefoxQueue = new SynchronousQueue<>();
    SynchronousQueue<Integer> ieQueue = new SynchronousQueue<>();

    Runnable ieRunnable = () -> {
        try {
            firefoxQueue.take();
            System.out.println("ieRunnable section 1");
            ieQueue.put(1);
            firefoxQueue.take();
            System.out.println("ieRunnable section 2");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    };

    Runnable firefoxRunnable = () -> {
        try {
            chromeQueue.take();
            System.out.println("firefoxRunnable section 1");
            firefoxQueue.put(1);
            chromeQueue.take();
            System.out.println("firefoxRunnable section 2");
            firefoxQueue.put(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    };

    Runnable chromeRunnable = () -> {
        try {
            ieQueue.take();//waits    
            System.out.println("chromeRunnable section 1");
            chromeQueue.put(1);//starts firefoxRunnable
            ieQueue.take();//wait for ieRunnable to signal
            System.out.println("chromeRunnable section 2");
            chromeQueue.put(1);//makes firefoxRunnable to continue
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    };
    executor.execute(chromeRunnable);
    executor.execute(firefoxRunnable);
    executor.execute(ieRunnable);
    ieQueue.put(1);
    executor.shutdown();
    try {
        if (!executor.awaitTermination(5, TimeUnit.MINUTES)) {
            executor.shutdownNow();
        }
    } catch (InterruptedException e) {
        executor.shutdownNow();
    }
}
@测试
void test()抛出InterruptedException{
ExecutorService executor=Executors.newFixedThreadPool(3);
SynchronousQueue chromeQueue=新的SynchronousQueue();
SynchronousQueue firefoxQueue=新的SynchronousQueue();
SynchronousQueue ieQueue=新的SynchronousQueue();
Runnable ieRunnable=()->{
试一试{
firefoxQueue.take();
System.out.println(“未命名的第1节”);
ieQueue.put(1);
firefoxQueue.take();
System.out.println(“第2节不可命名”);
}捕捉(中断异常e){
Thread.currentThread().interrupt();
}
};
Runnable firefoxRunnable=()->{
试一试{
chromeQueue.take();
System.out.println(“firefoxRunnable第1节”);
firefoxQueue.put(1);
chromeQueue.take();
System.out.println(“firefoxRunnable第2节”);
firefoxQueue.put(1);
}捕捉(中断异常e){
e、 printStackTrace();
}
};
可运行色度表=()->{
试一试{
ieQueue.take();//等待
系统输出打印LN(“色度可打印第1节”);
chromeQueue.put(1);//启动firefoxRunnable
ieQueue.take();//等待ieRunnable发出信号
System.out.println(“chromeRunnable第2节”);
chromeQueue.put(1);//使firefoxRunnable继续
}捕捉(中断异常e){
e、 printStackTrace();
}
};
执行人。执行人(chromeRunnable);
执行人。执行(firefoxRunnable);
执行人。执行人(不可命名);
ieQueue.put(1);
executor.shutdown();
试一试{
如果(!执行者等待终止(5,时间单位分钟)){
执行者。关机现在();
}
}捕捉(中断异常e){
执行者。关机现在();
}
}

这是我对这个问题的解决方案。我认为它背后的想法接近马特的解决方案。谢谢大家

 @Test
void test() throws InterruptedException {
    ExecutorService executor = Executors.newFixedThreadPool(3);
    SynchronousQueue<Integer> chromeQueue = new SynchronousQueue<>();
    SynchronousQueue<Integer> firefoxQueue = new SynchronousQueue<>();
    SynchronousQueue<Integer> ieQueue = new SynchronousQueue<>();

    Runnable ieRunnable = () -> {
        try {
            firefoxQueue.take();
            System.out.println("ieRunnable section 1");
            ieQueue.put(1);
            firefoxQueue.take();
            System.out.println("ieRunnable section 2");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    };

    Runnable firefoxRunnable = () -> {
        try {
            chromeQueue.take();
            System.out.println("firefoxRunnable section 1");
            firefoxQueue.put(1);
            chromeQueue.take();
            System.out.println("firefoxRunnable section 2");
            firefoxQueue.put(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    };

    Runnable chromeRunnable = () -> {
        try {
            ieQueue.take();//waits    
            System.out.println("chromeRunnable section 1");
            chromeQueue.put(1);//starts firefoxRunnable
            ieQueue.take();//wait for ieRunnable to signal
            System.out.println("chromeRunnable section 2");
            chromeQueue.put(1);//makes firefoxRunnable to continue
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    };
    executor.execute(chromeRunnable);
    executor.execute(firefoxRunnable);
    executor.execute(ieRunnable);
    ieQueue.put(1);
    executor.shutdown();
    try {
        if (!executor.awaitTermination(5, TimeUnit.MINUTES)) {
            executor.shutdownNow();
        }
    } catch (InterruptedException e) {
        executor.shutdownNow();
    }
}
@测试
void test()抛出InterruptedException{
ExecutorService executor=Executors.newFixedThreadPool(3);
SynchronousQueue chromeQueue=新的SynchronousQueue();
SynchronousQueue firefoxQueue=新的SynchronousQueue();
SynchronousQueue ieQueue=新的SynchronousQueue();
Runnable ieRunnable=()->{
试一试{
firefoxQueue.take();
System.out.println(“未命名的第1节”);
ieQueue.put(1);
firefoxQueue.take();
System.out.println(“第2节不可命名”);
}捕捉(中断异常e){
Thread.currentThread().interrupt();
}
};
Runnable firefoxRunnable=()->{
试一试{
chromeQueue.take();
System.out.println(“firefoxRunnable第1节”);
firefoxQueue.put(1);
chromeQueue.take();
System.out.println(“firefoxRunnable第2节”);
firefoxQueue.put(1);
}捕捉(中断异常e){
e、 printStackTrace();
}
};
可运行色度表=()->{
试一试{
ieQueue.take();//等待
系统输出打印LN(“色度可打印第1节”);
chromeQueue.put(1);//启动firefoxRunnable
ieQueue.take();//等待ieRunnable发出信号
System.out.println(“chromeRunnable第2节”);
chromeQueue.put(1);//使火燃烧