文章目录 ResortSeqDemo.java SingletonDemo.java SingletonDemo2.java VolatileDemo.java
ResortSeqDemo.java
package com. xd. thread ; public class ResortSeqDemo { volatile int a= 0 ; boolean flag= false ; public void method01 ( ) { a= 1 ; flag= true ; } public void method02 ( ) { if ( flag) { a+= 5 ; System . out. println ( "******最终值a:" + a) ; } } public static void main ( String [ ] args) { ResortSeqDemo resortSeq = new ResortSeqDemo ( ) ; new Thread ( ( ) -> { resortSeq. method01 ( ) ; } , "ThreadA" ) . start ( ) ; new Thread ( ( ) -> { resortSeq. method02 ( ) ; } , "ThreadB" ) . start ( ) ; } }
SingletonDemo.java
package com. xd. thread ; class Singleton1 { private static Singleton1 instance = null ; private Singleton1 ( ) { System . out. println ( Thread . currentThread ( ) . getName ( ) + "\t Singleton1构造方法执行了" ) ; } public static Singleton1 getInstance ( ) { if ( instance == null ) { instance = new Singleton1 ( ) ; } return instance; } } public class SingletonDemo { public static void main ( String [ ] args) {
for ( int i = 0 ; i < 10 ; i++ ) { new Thread ( ( ) -> { Singleton1 . getInstance ( ) ; } , String . valueOf ( i) ) . start ( ) ; } } }
SingletonDemo2.java
package com. xd. thread ; class Singleton2 { private volatile static Singleton2 instance = null ; private Singleton2 ( ) { System . out. println ( Thread . currentThread ( ) . getName ( ) + "\t Singleton2构造方法执行了" ) ; } public static Singleton2 getInstance ( ) { if ( instance == null ) { synchronized ( Singleton2 . class ) { if ( instance == null ) { instance = new Singleton2 ( ) ; } } } return instance; } } public class SingletonDemo2 { public static void main ( String [ ] args) {
for ( int i = 0 ; i < 10 ; i++ ) { new Thread ( ( ) -> { Singleton2 . getInstance ( ) ; } , String . valueOf ( i) ) . start ( ) ; } } }
VolatileDemo.java
package com. xd. thread ; import java. util. concurrent. TimeUnit ;
import java. util. concurrent. atomic. AtomicInteger ; class MyData { volatile int number = 0 ; AtomicInteger atomicInteger = new AtomicInteger ( ) ; public void setNumber ( ) { this . number = 60 ; } public void addPlusPlus ( ) { number++ ; } public void atomicPlusPlis ( ) { atomicInteger. getAndIncrement ( ) ; } } public class VolatileDemo { public static void main ( String [ ] args) { System . out. println ( "原子性操作..." ) ; MyData myData = new MyData ( ) ; for ( int i = 1 ; i <= 20 ; i++ ) { new Thread ( ( ) -> { for ( int j = 0 ; j < 1000 ; j++ ) { myData. addPlusPlus ( ) ; myData. atomicPlusPlis ( ) ; } } , String . valueOf ( i) ) . start ( ) ; } while ( Thread . activeCount ( ) > 2 ) { Thread . yield ( ) ; } System . out. println ( Thread . currentThread ( ) . getName ( ) + "\t int类型的最终的number值====" + myData. number) ; System . out. println ( Thread . currentThread ( ) . getName ( ) + "\t atomicInteger类型的最终的number值====" + myData. atomicInteger) ; } private static void method01 ( ) { System . out. println ( "可见性的演示。。。" ) ; MyData myData = new MyData ( ) ; new Thread ( ( ) -> { System . out. println ( Thread . currentThread ( ) . getName ( ) + "\t 执行。。。" ) ; try { TimeUnit . SECONDS . sleep ( 3 ) ; } catch ( InterruptedException e) { e. printStackTrace ( ) ; } myData. setNumber ( ) ; } , "ThreadA" ) . start ( ) ; while ( myData. number == 0 ) { System . out. println ( "main。。。" ) ; } System . out. println ( Thread . currentThread ( ) . getName ( ) + "\t main线程获取到了" + myData. number) ; } }