两个线程交替打印
如何让两个线程交替打印内容,比如A1B2C3D4,其中一个线程打印字母,另一个线程打印数字,交替完成输出
方式一:wait-notify
public class Test {
private static char [ ] charsArr1 = "12345" . toCharArray ( ) ;
private static char [ ] charsArr2 = "ABCDE" . toCharArray ( ) ;
private static Object lock = new Object ( ) ;
public static void main ( String[ ] args) {
new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
synchronized ( lock) {
for ( int i = 0 ; i < charsArr1. length; i++ ) {
System. out. print ( charsArr1[ i] ) ;
try {
lock. notify ( ) ;
lock. wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
lock. notify ( ) ;
}
}
} ) . start ( ) ;
new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
synchronized ( lock) {
for ( int i = 0 ; i < charsArr2. length; i++ ) {
System. out. print ( charsArr2[ i] ) ;
try {
lock. notify ( ) ;
lock. wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
lock. notify ( ) ;
}
}
} ) . start ( ) ;
}
}
方式二:lock-condition
public class Test {
private static char [ ] charsArr1 = "12345" . toCharArray ( ) ;
private static char [ ] charsArr2 = "ABCDE" . toCharArray ( ) ;
private static Lock lock = new ReentrantLock ( ) ;
private static Condition condition1 = lock. newCondition ( ) ;
private static Condition condition2 = lock. newCondition ( ) ;
public static void main ( String[ ] args) {
new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
lock. lock ( ) ;
try {
for ( int i = 0 ; i < charsArr1. length; i++ ) {
System. out. print ( charsArr1[ i] ) ;
condition2. signal ( ) ;
condition1. await ( ) ;
}
condition2. signal ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
} finally {
lock. unlock ( ) ;
}
}
} ) . start ( ) ;
new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
lock. lock ( ) ;
try {
for ( int i = 0 ; i < charsArr2. length; i++ ) {
System. out. print ( charsArr2[ i] ) ;
condition1. signal ( ) ;
condition2. await ( ) ;
}
condition1. signal ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
} finally {
lock. unlock ( ) ;
}
}
} ) . start ( ) ;
}
}
方式三:原子变量
public class Test {
private static AtomicInteger integer = new AtomicInteger ( 0 ) ;
private static char [ ] charsArr1 = "12345" . toCharArray ( ) ;
private static char [ ] charsArr2 = "ABCDE" . toCharArray ( ) ;
public static void main ( String[ ] args) {
new Thread ( ( ) - > {
for ( int i = 0 ; i < charsArr1. length; ) {
if ( integer. get ( ) % 2 == 0 ) {
System. out. print ( charsArr1[ i++ ] ) ;
integer. getAndIncrement ( ) ;
}
}
} ) . start ( ) ;
new Thread ( ( ) - > {
for ( int i = 0 ; i < charsArr2. length; ) {
if ( integer. get ( ) % 2 == 1 ) {
System. out. print ( charsArr2[ i++ ] ) ;
integer. getAndIncrement ( ) ;
}
}
} ) . start ( ) ;
}
}
方式四:LockSupport
LockSupport可以park挂起一个线程,也可以unpark给线程解挂,并且可以提前释放一个许可,后面的线程就不会park,详情可以参考AQS工具-LockSupport
public class Test {
private static char [ ] charsArr1 = "12345" . toCharArray ( ) ;
private static char [ ] charsArr2 = "ABCDE" . toCharArray ( ) ;
static Thread t1 = null, t2 = null;
public static void main ( String[ ] args) {
t1 = new Thread ( ( ) - > {
for ( int i = 0 ; i < charsArr1. length; ) {
System. out. print ( charsArr1[ i++ ] ) ;
LockSupport. unpark ( t2) ;
LockSupport. park ( ) ;
}
} ) ;
t2 = new Thread ( ( ) - > {
for ( int i = 0 ; i < charsArr2. length; ) {
LockSupport. park ( ) ;
System. out. print ( charsArr2[ i++ ] ) ;
LockSupport. unpark ( t1) ;
}
} ) ;
t1. start ( ) ;
t2. start ( ) ;
}
}