题目:一个线程打印 1~52,另一个线程打印字母A-Z。打印顺序为12A34B56C……5152Z。
解决的方法有很多种,比如:
使用synchronized, wait和notifyAll
使用Lock 和 Condition
使用Semaphore 等。
本文采用Lock 和 Condition来实现。
程序源代码:
package my.thread.test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 一个线程打印 1~52,另一个线程打印字母A-Z。打印顺序为12A34B56C……5152Z。
*
* @author Eric
*
*/
public class ThreadCommunicationTest {
private final Lock lock = new ReentrantLock();
private final Condition conditionA = lock.newCondition();
private final Condition conditionB = lock.newCondition();
private static char currentThread = 'A';
public static void main(String[] args) {
ThreadCommunicationTest test = new ThreadCommunicationTest();
ExecutorService service = Executors.newCachedThreadPool();
service.execute(test.new RunnableA());
service.execute(test.new RunnableB());
service.shutdown();
}
private class RunnableA implements Runnable {
public void run() {
for (int i = 1; i <= 52; i++) {
lock.lock();
try {
while (currentThread != 'A') {
try {
conditionA.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(i);
if (i % 2 == 0) {
currentThread = 'B';
conditionB.signal();
}
} finally {
lock.unlock();
}
}
}
}
private class RunnableB implements Runnable {
@Override
public void run() {
for (char c = 'A'; c <= 'Z'; c++) {
lock.lock();
try {
while (currentThread != 'B') {
try {
conditionB.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(c);
currentThread = 'A';
conditionA.signal();
} finally {
lock.unlock();
}
}
}
}
}
程序运行结果:
1
2
A
3
4
B
5
6
C
7
8
D
9
10
E
11
12
F
13
14
G
15
16
H
17
18
I
19
20
J
21
22
K
23
24
L
25
26
M
27
28
N
29
30
O
31
32
P
33
34
Q
35
36
R
37
38
S
39
40
T
41
42
U
43
44
V
45
46
W
47
48
X
49
50
Y
51
52
Z
分享到:
相关推荐
1. 写两个线程,一个线程打印1-52,另一个线程打印字母A-Z。打印顺序为12A34B56C……5152Z。 学习线程之间的通信协调关系。 2. 使用 runnable 接口实现按两个不同的时间间隔( 1 秒和 3 秒)在屏幕上显示当前时间。...
1-客户端健壮性测试.81e0e000-bc85-11e7-9a52-9bd0f40b56c0.pdf
4-自动化体系建设.66e4f510-bc86-11e7-9a52-9bd0f40b56c0.pdf
其中a为1至9之中的一个数,项数也要可以指定。 import java.util.Scanner; class Multinomial{ public static void main(String[] args){ int a; //定义输入的 a int howMany; //定义最后的一项有多少个数字 ...
2-问道移动测试在线录制技术.d39ea030-bc85-11e7-9a52-9bd0f40b56c0.pdf
1.程序中的else子句总是与离它最近的前面一个if子句配对 2.变量一定要赋值 第五课 条件表达式 1.<表达式1>?<表达式2>:<表达式3> 第六课 switch语句 1执行过程为:先计算表达式的值,然后从前往后依次与case...
Java开发工程师面试题通过多线程的wait()、和notify()实现12A34B56C78D910操作
xde_nk\33e56942-4f86-4b44-a9cd-1b2a84b508a8.dsm - xde_nk - 7.0.0.6077 OEMXIPKERNEL\19d4441e-9352-8e98-266c-f387d94e0f4f.dsm - OEMXIPKERNEL - 7.0.0.6077 MSXIPKERNELLTK\42d1aab7-6dfa-4c03-9797-d66f...
AIR-CT2500-K9-8-5-182-0.aes WLC 2504固件,配合AIR-CT...SHA512 Checksum : 8de868143326d027b175f99801287b7e65d0d4dced47c40a77fd7142d2eded1cceeda53e17813e13efaa4d27bd6eebc4158f9e9c23965f2f64b56f291b623486
{7067a92c-1db4-4e5e-869c-25f841287f8b} {752a85d4-68d6-48ae-ab7d-6640f5f75d85} {7f57cf46-4467-4c2d-adfa-0cba7c507e54} {8620c15f-30dc-4dba-a131-7c5d20cf4a29} {8E812B7E-0FF3-11DD-9194-8F9555D89593} {8f8...
-<macro name="F12" guid="{7016AAD1-1A1C-4301-9441-1DB8A854E81E}" hidden="true" color="4278246655" original="true"> -返回" guid="{7AF3E12D-61FB-415C-9AB8-BFA1CD833488}" hidden="false" color=...
网络设备
1、可以在后台录制鼠标键盘所有动作。 2、可以回放所有动作。 3、可以将鼠标键盘动作记录成文件,在任何时候重新播放。 4、可以记录键盘所有按键,然后形成文件。 5、可以更换18种皮肤,是您最好的选择,让您...
b56行 消防应急预案流程图.xlsx
大屏展示后台数据可视化UI界面,包含PSD源文件,方便使用。 大屏展示后台数据可视化UI界面,包含PSD源文件,方便使用。
object Form1: TForm1 Left = 312 Top = 126 Width = 459 Height = 477 Caption = #20116#23376#26827 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 ...
This standard establishes a color bar signal for the UHDTV video format specified in ARIB standard STD-B56 to facilitate video level control and monitor adjustment.
SurfacePro3_BMR_40_7.102.0.zip 官网原链 ...-442a...百度云盘 链接:https://pan.baidu.com/s/1lwQkgvBdB1MJI_u-ll-qHw 提取码
草图 项目 使用RNN使用Theano块... Running experiment handwriting-1X900m20d5r13b56e15G10 ... Epoch 1, step 105 test_sequence_log_likelihood: -631.519287109 python sketch.py --dim 900 --depth 1 --bs 56
enandroid-support-v4-2b2ce039b11e2129b57777e41c315af5.jar