解题思路:
在我看来要实现两个线程交替的打印,
1.t1线程,打印数字1,然后唤醒t2线程,
2.t1线程自己睡眠
3.t2线程打印字母,A,然后唤醒t1线程
上述步骤重复执行
首先用** LockSupport.unpark() LockSupport.park()**的方案来实现,t1、t2 线程必须相互持有,实现代码如下:
public class Test6 {
static String s1 = "123456";
static String s2 = "ABCDEFG";
public static void main(String[] args) throws InterruptedException {
Thread t1 = null;
Thread t2 = null;
MyRunable myRunable2 = new MyRunable();
MyRunable myRunable1 = new MyRunable();
t1 = new Thread(myRunable2);
t2 = new Thread(myRunable1);
myRunable2.setOther(t2);
myRunable2.setS(s2);
myRunable1.setOther(t1);
myRunable1.setS(s1);
t2.start();
Thread.sleep(10);
t1.start();
}
static class MyRunable implements Runnable{
p