栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现两个线程交替打印的实例代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实现两个线程交替打印的实例代码

使用ReentrantLock实现两个线程交替打印

实现字母在前数字在后

package com.study.pattern;

 

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

 

public class Demo2 {

 private static Lock lock = new ReentrantLock();

 private static Condition c1 = lock.newCondition();

 private static Condition c2 = lock.newCondition();

 private static CountDownLatch count = new CountDownLatch(1);

 public static void main(String[] args) {

  String c = "ABCDEFGHI";

  char[] ca = c.toCharArray();

  String n = "123456789";

  char[] na = n.toCharArray();

  Thread t1 = new Thread(() -> {

   try {

    lock.lock();

    count.countDown();

    for(char caa : ca) {

     c1.signal();

     System.out.print(caa);

     c2.await();

    }

    c1.signal();

   } catch (InterruptedException e) {

    e.printStackTrace();

   } finally {

    lock.unlock();

   }

  });

  Thread t2 = new Thread(() -> {

   try {

    count.await();

    lock.lock();

    for(char naa : na) {

     c2.signal();

     System.out.print(naa);

     c1.await();

    }

    c2.signal();

   } catch (InterruptedException e) {

    e.printStackTrace();

   } finally {

    lock.unlock();

   }

  });

  t1.start();

  t2.start();

 }

}

最后输出结果:

使用linkedTransferQueue实现两个线程交替打印

实现字母在前数字在后

package com.study.pattern;

 

 

import java.util.concurrent.linkedTransferQueue;

 

public class Demo3 {

 private static linkedTransferQueue linkedC = new linkedTransferQueue();

 private static linkedTransferQueue linkedN = new linkedTransferQueue();

 public static void main(String[] args) {

  String c = "ABCDEFGHI";

  char[] ca = c.toCharArray();

  String n = "123456789";

  char[] na = n.toCharArray();

  Thread t1 = new Thread(() -> {

   for(char caa : ca) {

    try {

     linkedC.put(caa);

     char a = linkedN.take();

     System.out.print(a);

    } catch (InterruptedException e) {

     e.printStackTrace();

    }

   }

  });

  Thread t2 = new Thread(() -> {

   for(char naa : na) {

    try {

     char b = linkedC.take();

     System.out.print(b);

     linkedN.put(naa);

    } catch (InterruptedException e) {

     e.printStackTrace();

    }

   }

  });

  t1.start();

  t2.start();

 

 }

}

输出结果:

使用synchronized实现两个线程交替打印

实现字母在前数字在后

package com.study.pattern;

 

 

import java.util.concurrent.CountDownLatch;

 

public class Demo4 {

 private static CountDownLatch count = new CountDownLatch(1);

 public static void main(String[] args) {

  String c = "ABCDEFGHI";

  char[] ca = c.toCharArray();

  String n = "123456789";

  char[] na = n.toCharArray();

  Object lock = new Object();

  Thread t1 = new Thread(() -> {

   synchronized (lock) {

    count.countDown();

    for(char caa : ca) {

     System.out.print(caa);

     lock.notify();

     try {

      lock.wait();

     } catch (InterruptedException e) {

      e.printStackTrace();

     }

    }

    lock.notify();

   }

  });

  Thread t2 = new Thread(() -> {

   try {

    count.await();

   } catch (InterruptedException e) {

    e.printStackTrace();

   }

   synchronized (lock) {

    for(char naa : na) {

     System.out.print(naa);

     lock.notify();

     try {

      lock.wait();

     } catch (InterruptedException e) {

      e.printStackTrace();

     }

    }

    lock.notify();

   }

  });

  t1.start();

  t2.start();

 }

}

输出结果:

使用LockSupport实现两个线程交替打印

实现字母在前数字在后

package com.study.pattern;

 

 

import java.util.concurrent.locks.LockSupport;

 

public class Demo5 {

 private static Thread t1;

 private static Thread t2;

 public static void main(String[] args) {

  String c = "ABCDEFGHI";

  char[] ca = c.toCharArray();

  String n = "123456789";

  char[] na = n.toCharArray();

  t1 = new Thread(() -> {

   for(char caa : ca) {

    System.out.print(caa);

    LockSupport.unpark(t2);

    LockSupport.park();

 

   }

  });

  t2 = new Thread(() -> {

   for(char naa : na) {

    LockSupport.park();

    System.out.print(naa);

    LockSupport.unpark(t1);

   }

  });

  t1.start();

  t2.start();

 }

}

输出结果:

以上就是java实现两个线程交替打印的详细内容,感谢大家的学习和对考高分网的支持。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/135702.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号