实现字母在前数字在后
这个方法真的有很多很多种
1.利用信号量
import java.util.concurrent.*;
/**
* @Author: 软件171 邱忠玉 201707163
* @Date: 2020/9/20 10:04
*/
public class Main {
private static Thread thread1;
private static Thread thread2 = null;
public static void main(String[] args) {
String c = "ABCDEFGHI";
char[] ca = c.toCharArray();
String n = "123456789";
char[] na = n.toCharArray();
Semaphore semaphore = new Semaphore(0);
Semaphore semaphore2 = new Semaphore(0);
thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (char a : ca) {
System.out.println(a);
semaphore2.release();
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread2 = new Thread(new Runnable() {
@Override
public void run() {
for (char a : na) {
try {
semaphore2.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(a);
semaphore.release();
}
}
});
thread1.start();
thread2.start();
}
}
2.利用阻塞队列
import java.util.concurrent.*;
/**
* @Author: 软件171 邱忠玉 201707163
* @Date: 2020/9/20 10:04
*/
public class Main {
private static LinkedTransferQueue<Character> linkedC = new LinkedTransferQueue<Character>();
private static LinkedTransferQueue<Character> linkedN = new LinkedTransferQueue<Character>();
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();
}
}
3.利用LockSupport
import java.util.concurrent.locks.LockSupport;
/**
* @Author: 软件171 邱忠玉 201707163
* @Date: 2020/9/20 10:04
*/
public class Main {
private static Thread thread1;
private static Thread thread2 = null;
public static void main(String[] args) {
String c = "ABCDEFGHI";
char[] ca = c.toCharArray();
String n = "123456789";
char[] na = n.toCharArray();
thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (char a : ca) {
System.out.println(a);
LockSupport.unpark(thread2);
LockSupport.park();
}
}
});
thread2 = new Thread(new Runnable() {
@Override
public void run() {
for (char a : na) {
LockSupport.park();
System.out.println(a);
LockSupport.unpark(thread1);
}
}
});
thread1.start();
thread2.start();
}
}
4.利用Synchronized
import java.util.concurrent.*;
/**
* @Author: 软件171 邱忠玉 201707163
* @Date: 2020/9/20 10:04
*/
public class Main {
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();
}
}
5.利用Lock
public class Main {
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) {
System.out.print(caa);
c1.signal();
c2.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
});
Thread t2 = new Thread(() -> {
try {
count.await();
lock.lock();
for (char naa : na) {
System.out.print(naa);
c2.signal();
c1.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
});
t1.start();
t2.start();
}
}