synchronized 消费者生产者案例
public class ThreadDemo {
public static void main ( String[ ] args) {
Resourse resourse = new Resourse ( ) ;
new Thread ( ( ) - > { try { for ( int i = 0 ; i < 10 ; i++ ) { resourse. produce ( ) ; Thread. sleep ( 500 ) ; } } catch ( Exception e) { e. printStackTrace ( ) ; } } ) . start ( ) ;
new Thread ( ( ) - > { try { for ( int i = 0 ; i < 10 ; i++ ) { resourse. consumption ( ) ; Thread. sleep ( 1000 ) ; } } catch ( Exception e) { e. printStackTrace ( ) ; } } ) . start ( ) ;
new Thread ( ( ) - > { try { for ( int i = 0 ; i < 10 ; i++ ) { resourse. produce ( ) ; Thread. sleep ( 500 ) ; } } catch ( Exception e) { e. printStackTrace ( ) ; } } ) . start ( ) ;
new Thread ( ( ) - > { try { for ( int i = 0 ; i < 10 ; i++ ) { resourse. consumption ( ) ; Thread. sleep ( 2000 ) ; } } catch ( Exception e) { e. printStackTrace ( ) ; } } ) . start ( ) ;
}
static class Resourse {
private Integer num = 0 ;
public synchronized void consumption ( ) throws Exception {
while ( num== 0 ) {
this . wait ( ) ;
}
System. out. println ( Thread. currentThread ( ) . getName ( ) + ":当前为" + this . num+ " 消费后:" + -- this . num) ;
this . notifyAll ( ) ;
}
public synchronized void produce ( ) throws Exception {
while ( num!= 0 ) {
this . wait ( ) ;
}
System. out. println ( Thread. currentThread ( ) . getName ( ) + ":当前为" + this . num+ " 生产后:" + ++ this . num) ;
this . notifyAll ( ) ;
}
}
}
Lock 生产者消费者案例
public class ThreadLock {
public static void main ( String[ ] args) {
Resourse resourse = new Resourse ( ) ;
new Thread ( ( ) - > {
for ( int i = 0 ; i < 10 ; i++ ) {
resourse. consumer ( ) ;
}
} ) . start ( ) ;
new Thread ( ( ) - > {
for ( int i = 0 ; i < 10 ; i++ ) {
resourse. produce ( ) ;
}
} ) . start ( ) ;
new Thread ( ( ) - > {
for ( int i = 0 ; i < 10 ; i++ ) {
resourse. consumer ( ) ;
}
} ) . start ( ) ;
new Thread ( ( ) - > {
for ( int i = 0 ; i < 10 ; i++ ) {
resourse. produce ( ) ;
}
} ) . start ( ) ;
}
static class Resourse {
private Integer num = 0 ;
private Lock lock = new ReentrantLock ( ) ;
private Condition condition = lock. newCondition ( ) ;
public void consumer ( ) {
lock. lock ( ) ;
try {
while ( num== 0 ) {
condition. await ( ) ;
}
System. out. println ( Thread. currentThread ( ) . getName ( ) + ":当前为" + this . num+ " 消费后:" + -- this . num) ;
condition. signalAll ( ) ;
} catch ( Exception e) {
} finally {
lock. unlock ( ) ;
}
}
public void produce ( ) {
lock. lock ( ) ;
try {
while ( num!= 0 ) {
condition. await ( ) ;
}
System. out. println ( Thread. currentThread ( ) . getName ( ) + ":当前为" + this . num+ " 生产后:" + ++ this . num) ;
condition. signalAll ( ) ;
} catch ( Exception e) {
} finally {
lock. unlock ( ) ;
}
}
}
}
Lock加Condition精准唤醒案例
public class ThreadLockCondition {
public static void main ( String[ ] args) {
Resourse resourse = new Resourse ( ) ;
new Thread ( ( ) - > {
for ( int i = 0 ; i < 10 ; i++ ) {
try {
resourse. print ( 1 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
} , "A" ) . start ( ) ;
new Thread ( ( ) - > {
for ( int i = 0 ; i < 10 ; i++ ) {
try {
resourse. print ( 2 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
} , "B" ) . start ( ) ;
new Thread ( ( ) - > {
for ( int i = 0 ; i < 10 ; i++ ) {
try {
resourse. print ( 3 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
} , "C" ) . start ( ) ;
}
static class Resourse {
private Integer number = 1 ;
private Lock lock = new ReentrantLock ( ) ;
private Condition condition1 = lock. newCondition ( ) ;
private Condition condition2 = lock. newCondition ( ) ;
private Condition condition3 = lock. newCondition ( ) ;
public void print ( int sign) throws InterruptedException {
if ( sign == 1 ) {
lock. lock ( ) ;
try {
while ( number!= 1 ) {
condition1. await ( ) ;
}
for ( int i = 0 ; i < 5 ; i++ ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + ":" + i) ;
}
number = 2 ;
condition2. signal ( ) ;
} catch ( Exception e) {
} finally {
lock. unlock ( ) ;
}
} else if ( sign == 2 ) {
lock. lock ( ) ;
try {
while ( number!= 2 ) {
condition2. await ( ) ;
}
for ( int i = 0 ; i < 10 ; i++ ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + ":" + i) ;
}
number = 3 ;
condition3. signal ( ) ;
} catch ( Exception e) {
} finally {
lock. unlock ( ) ;
}
} else if ( sign == 3 ) {
lock. lock ( ) ;
try {
while ( number!= 3 ) {
condition3. await ( ) ;
}
for ( int i = 0 ; i < 15 ; i++ ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + ":" + i) ;
}
number = 1 ;
condition1. signal ( ) ;
} catch ( Exception e) {
} finally {
lock. unlock ( ) ;
}
}
}
}
}
synchronized 8锁案例加原理
public class Lock8 {
static class Resourse {
public synchronized void findAll ( ) {
try {
TimeUnit. SECONDS. sleep ( 2 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System. out. println ( "findAll...." ) ;
}
public static synchronized void save ( ) {
System. out. println ( "save...." ) ;
}
public void hello ( ) {
System. out. println ( "say hello" ) ;
}
}
public static void main ( String[ ] args) throws Exception {
Resourse resourse = new Resourse ( ) ;
Resourse resourse2 = new Resourse ( ) ;
new Thread ( ( ) - > {
resourse. findAll ( ) ;
} ) . start ( ) ;
TimeUnit. SECONDS. sleep ( 1 ) ;
new Thread ( ( ) - > {
resourse2. save ( ) ;
} ) . start ( ) ;
}
}