用两个线程,一个输出字母,一个输出数字,交替输出1A2B3C4D...26Z

方法一

public class Test {

static Thread t1 = null, t2 = null;

public static void main(String[] args) {

char[] aI = "1234567".toCharArray();

char[] aC = "ABCDEFG".toCharArray();

t1 = new Thread(new Runnable() {

@Override

public void run() {

for (char c : aI) {

System.out.print(c);

LockSupport.unpark(t2);

LockSupport.park();

}

}

});

t2 = new Thread(new Runnable() {

@Override

public void run() {

for (char c : aC) {

LockSupport.park();

System.out.print(c);

LockSupport.unpark(t1);

}

}

});

t1.start();

t2.start();

}

}

方法二

public class Test2 {

static Thread t1 = null, t2 = null;

enum ReadyToRun {T1, T2}

static ReadyToRun r = T1;

public static void main(String[] args) {

char[] aI = "1234567".toCharArray();

char[] aC = "ABCDEFG".toCharArray();

t1 = new Thread(new Runnable() {

@Override

public void run() {

for (char c : aI) {

while (r != T1) {

}

System.out.print(c);

r = T2;

}

}

});

t2 = new Thread(new Runnable() {

@Override

public void run() {

for (char c : aC) {

while (r != T2) {

}

System.out.print(c);

r = T1;

}

}

});

t1.start();

t2.start();

}

}

方法三

public class Test3 {

static Thread t1 = null, t2 = null;

static AtomicInteger a = new AtomicInteger(1);

public static void main(String[] args) {

char[] aI = "1234567".toCharArray();

char[] aC = "ABCDEFG".toCharArray();

t1 = new Thread(new Runnable() {

@Override

public void run() {

for (char c : aI) {

while (a.get() != 1) {

}

System.out.print(c);

a.set(2);

}

}

});

t2 = new Thread(new Runnable() {

@Override

public void run() {

for (char c : aC) {

while (a.get() != 2) {

}

System.out.print(c);

a.set(1);

}

}

});

t1.start();

t2.start();

}

}

方法四

public class Test4 {

//队列阻塞特性

static Thread t1 = null, t2 = null;

static BlockingQueue bq1 = new ArrayBlockingQueue<>(1);

static BlockingQueue bq2 = new ArrayBlockingQueue<>(1);

public static void main(String[] args) {

char[] aI = "1234567".toCharArray();

char[] aC = "ABCDEFG".toCharArray();

t1 = new Thread(new Runnable() {

@Override

public void run() {

for (char c : aI) {

System.out.print(c);

try {

bq1.put("ok");

bq2.take();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

});

t2 = new Thread(new Runnable() {

@Override

public void run() {

for (char c : aC) {

try {

bq1.take();

System.out.print(c);

bq2.put("ok");

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

});

t2.start();

t1.start();

}

}

方法五

public class Test5 {

static Thread t1 = null, t2 = null;

static CountDownLatch latch = new CountDownLatch(1);

public static void main(String[] args) {

char[] aI = "1234567".toCharArray();

char[] aC = "ABCDEFG".toCharArray();

final Object o = new Object();

t1 = new Thread(new Runnable() {

@Override

public void run() {

synchronized (o) {

for (char c : aI) {

System.out.print(c);

latch.countDown();

try {

o.notify();

o.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

o.notify();

}

}

});

t2 = new Thread(new Runnable() {

@Override

public void run() {

try {

latch.await();

} catch (InterruptedException e) {

e.printStackTrace();

}

synchronized (o) {

for (char c : aC) {

System.out.print(c);

try {

o.notify();

o.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

o.notify();

}

}

});

t1.start();

t2.start();

}

}

方法六

public class Test6 {

static Thread t1 = null, t2 = null;

public static void main(String[] args) {

char[] aI = "1234567".toCharArray();

char[] aC = "ABCDEFG".toCharArray();

Lock lock = new ReentrantLock();

//一把锁两种条件

Condition conditionT1 = lock.newCondition();

Condition conditionT2 = lock.newCondition();

CountDownLatch latch = new CountDownLatch(1);

t1 = new Thread(new Runnable() {

@Override

public void run() {

try {

lock.lock();

for (char c : aI) {

System.out.print(c);

latch.countDown();

conditionT2.signal();

conditionT1.await();

}

conditionT2.signal();

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

});

t2 = new Thread(new Runnable() {

@Override

public void run() {

try {

latch.await();

lock.lock();

for (char c : aC) {

System.out.print(c);

conditionT1.signal();

conditionT2.await();

}

conditionT1.signal();

} catch (Exception e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

});

t1.start();

t2.start();

}

}

java1a2b3c4d5e6f_用两个线程,一个输出字母,一个输出数字,交替输出1A2B3C4D...26Z...相关推荐

  1. 树莓派 | threading01 - 创建两个子线程同时运行,两个线程各负责控制一个LED灯以不同的频率闪烁

    文章目录 一.前言 二.代码 三.运行 一.前言 Python | threading01 - 创建两个同时运行的子线程 上一次使用了python的多线程库threading创建了两个同时运行的子线程 ...

  2. 写一个26字母和0-9数字 不同长度组合的输出

    写一个26字母和0-9数字 不同长度组合的输出,输出全部的时间不能超过2个小时.需要用多线程录 直接上代码 Password类 package com.more.util; import java.u ...

  3. c语言输出字母是问号,为什么数组输出会多一个问号

    公告: 为响应国家净网行动,部分内容已经删除,感谢读者理解. 话题:为什么数组输出会多一个问号回答:这个问题问的.是什么数组,int数组,char数组?什么叫多输出一个问好.最好把码贴出来,才知道是什 ...

  4. C语言输出字母小树,数字小树,**号小树

    1. 以下,是输出字母小树,及规律图解,及vs中使用是scanf的方法 ,(以下各片段都是一个程序里的代码片段) 首先得对输入数据 进行检查,(程序的健壮性) #define _CRT_SECURE_ ...

  5. c语言printf输出字母,C语言printf()格式化输出控制

    printf()函数是格式化输出函数,一般用于向标准输出设备按规定格式输出信息.在编写程序时经常会用到此函数.printf()函数的调用格式为: printf("", ); 其中格 ...

  6. C语言(增加难度)给定一个字母N,输出一个字母三角形,输入一个大写字母A-Z或1-9,输出三角形

    样例输入:4 样例输出: 样例输入:F 样例输出: 这组字母三角形比上一个要难不少,所以用C++进行编写,会简单一些 进行分析:这次不仅有字母的存在,而且还有数字,如何将他们统一进行处理.所以就要采用 ...

  7. 用Java写一个26字母和0-9数字 不同长度组合的输出,输出全部的时间不能超过2个小时。需要用多线程录

    直接上代码 public class MyThread extends Thread{char[] word = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g ...

  8. 密码转换 输入一个大写字母,把它往后移动两个位置加密,即A变C,B变D,X变Z,Z变B

    输入格式: 一行,一个大写字母 输出格式: 一行,一个大写字母 输入样例: D 输出样例: F #include<stdio.h> int main(void) { char a[100] ...

  9. 线程打印_面试题:用程序实现两个线程交替打印 0~100 的奇偶数

    作者:dadiyang来源:https://blog.csdn.net/dadiyang/article/details/88315124 面试场景 面试官:Java多线程了解吗?你给我写一下,起两个 ...

最新文章

  1. 路由器和宽带路由器故障汇总!
  2. ———— javascript中数组常用方法总结 ————
  3. 使用XSLT制作HTML邮件模板并发送
  4. Django从理论到实战(part35)--QuerySetAPI
  5. select scope_identity()
  6. php 2010excel,PHPExcel在php5.2.10上的bug
  7. 信号完整性与电源完整性分析_电源完整性,信号完整性,你说哪个更重要一点?...
  8. 开源项目的网址[不定时更新]
  9. 从傅里叶级数到傅里叶变换
  10. 一周“金”榜:CSDN创作者收益周排行榜(1月3日到1月9日)
  11. 《袁老师访谈录》第五期 | 史维教授/香港科大校长:【与香港科大一起群飞得更远!】...
  12. Eclipse基础知识
  13. Elasticsearch5基于completion suggester实现提示词(类京东淘宝)
  14. 空间大地测量与GPS导航定位时间系统相互转换,格里高利时通用时儒略日,GPS时,年积日相互转换
  15. KiBiEx互联网交易互动平台数字货币
  16. 35岁被大厂踢出豪门,褪去这层皮你还剩什么
  17. 苹果手机的siri在哪里_苹果手机Siri功能的设置和使用 原来是这样的
  18. 吉林大学计算机学院课程学分,吉林大学软件学院学分规定
  19. python selenium处理iframe和弹框(一)
  20. 【JS】BOM 详解(工作必备)

热门文章

  1. linux shell 自定义函数(定义、返回值、变量作用域)介绍
  2. vvv在线文档导出工具_一款真正实时的在线文档协作编辑工具,多人可同时对一份文件修改...
  3. Ubuntu中安装Eclipse的SVN插件——subclipse
  4. t oracle删除吗,Oracle 11g 手工建库与删库
  5. 输出整形变量语句_Python合集之Python变量
  6. 产品经理必须知道的一点知识:三种方法判断一个产品该不该做
  7. linux命令画圣诞树图片,以 Linux 的方式庆祝圣诞节
  8. java创建树结构_Java学习之XML-017
  9. 用C语言实现津巴布韦这道算法题?
  10. PHP文件加到WordPress页面,WordPress-所有页面链接到index.php文件