In Java, you can use synchronized volatile Atomic lock for multithreaded programming to achieve thread safety. This paper is to demonstrate and summarize these kinds of ways.
which
1. The use of volatile alone is no way to ensure thread safety
2. Use synchronized and lock to note the use of methods to create an instance of a lock object or define a synchronized method in the main process
3. The use of atomic atomic classes can also ensure thread safety.
/** * @Package * @Description * @author Chenj * @date 2015-9-14 afternoon 11:09:40 * @version V1.0 * * Import JAV
A.util.concurrent.executorservice;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.atomic.AtomicInteger;
Import Java.util.concurrent.locks.Lock;
Import Java.util.concurrent.locks.ReentrantLock; /** * * @author chenj|
2015-01-01 * */public class testmultithread{Object _object = new Object ();
static int i=0;
static Integer si = 0;
static volatile Integer VI = 0;
static Atomicinteger Autointeger = new Atomicinteger ();
static int synclassint = 0;
static int lockint = 0;
static int staticclassint = 0;
Static lock lock = new Reentrantlock ();
static Object _obj = new Object (); public void Testvolatileinteger (Thread _thread1,thread _thread2) throws exception{executorservice pool = Executor
S.newcachedthreadpool ();
Creating a Runnable interface object, thread object of course also implements the Runnable interface thread T1 = _thread1; Threadt2 = _thread2;
Put the thread into the pool for execution pool.execute (T1);
Pool.execute (T2);
Close the thread pool Pool.shutdown (); while (true) {if (pool.isterminated ()) {if (staticclassint!=0) {System.out.prin TLN ("All the child threads are finished.")
");
System.out.println ("i>>>>>" +i);
System.out.println ("vi>>>>>" +vi);
System.out.println ("si>>>>>" +si);
System.out.println ("autointeger>>>>>" +autointeger);
System.out.println ("synclassint>>>>>" +synclassint);
System.out.println ("lockint>>>>>" +lockint);
System.out.println ("staticclassint>>>>>" +staticclassint);
} break;
}} public synchronized static void Testsychronizedmethod () {i++; Static class Testsychronizedmethodclass extends thread{@Override public void Run () {for int k=0;k<2 00000; k++) {TestValue ();
}} public static void TestValue () {staticclassint++; } public static void Main (string[] args) throws exception{testmultithread Testmultithread = new Testmultithread
();
Testmultithread.testvolatileinteger (New Threadsychronizedmethead (), New Threadsychronizedmethead ());
Thread.Sleep (100);
Testmultithread.testvolatileinteger (New Threadstaticinteger (), New Threadstaticinteger ());
Thread.Sleep (100);
Testmultithread.testvolatileinteger (New Threadstaticvolatileinteger (), New Threadstaticvolatileinteger ());
Thread.Sleep (100);
Testmultithread.testvolatileinteger (New Threadatomicinteger (), New Threadatomicinteger ());
Thread.Sleep (100);
Testmultithread.testvolatileinteger (New Threadsychronizedobjmethead (), New Threadsychronizedobjmethead ());
Thread.Sleep (100);
Testmultithread.testvolatileinteger (New Threadlock (), New Threadlock ());
Thread.Sleep (100); Testmultithread.testvolatileinteger (NEW Testmultithread.testsychronizedmethodclass (), New Testmultithread.testsychronizedmethodclass ());
Thread.Sleep (100); } class Threadstaticvolatileinteger extends Thread {@Override public void run () {for int k=0;k<200000
; k++) {testmultithread.vi++; Class Threadatomicinteger extends Thread {@Override public void run () {for int k=0;k<200000;k
+ +) {TestMultiThread.autoInteger.incrementAndGet (); Class Threadstaticinteger extends Thread {@Override public void run () {for int k=0;k<200000;k
+ +) {testmultithread.si++; Class Threadsychronizedmethead extends Thread {@Override public void run () {for int k=0;k<200
000;k++) {Testmultithread.testsychronizedmethod ();
} public synchronized void Test () {testmultithread.i++; } class Threadsychronizedobjmethead extends Thread {@Override public void run () {for int k=0;k<200000;k++) {synchronized (testmultithread._obj) {testmultithread.synclassint++; Class Threadlock extends Thread {@Override public void run () {for int k=0;k<200000;k
+ +) {lockobj ();
} public void Lockobj () {TestMultiThread.lock.lock ();
try{Testmultithread.lockint + +;
}finally{TestMultiThread.lock.unlock (); }
}
}
Program Run Result:
All the child threads are finished.
i>>>>>400000
vi>>>>>257432
si>>>>>222170
autointeger>>>>>400000
synclassint>>>>>400000
lockint>>>>>400000
staticclassint>>>>>266997