用兩個(gè)線程板乙,一個(gè)輸出字母是偷,一個(gè)輸出數(shù)字,交替輸出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 {
//隊(duì)列阻塞特性
static Thread t1 = null, t2 = null;
static BlockingQueue<String> bq1 = new ArrayBlockingQueue<>(1);
static BlockingQueue<String> 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();
}
}