您的位置:首页 > 编程语言 > Java开发

java多线程 synchronized volatile Atomic LOCK的使用

2015-09-22 16:07 288 查看
java中可以使用 synchronized volatile Atomic LOCK进行多线程编程来实现线程安全。现对这几种方式进行演示与总结。

其中:

1. 单纯使用volatile是没有办法保证线程安全的

2. 使用synchronized和 lock要注意使用方法,要在主进程中创建lock对象的实例或定义synchronized方法

3. 使用Atomic 原子类也是可以保证线程安全的。

/**

* @Package
* @Description
* @author chenj
* @date 2015-9-14 下午11:09:40
* @version V1.0
*/

import java.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 = Executors.newCachedThreadPool();
//创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口
Thread t1 = _thread1;
Thread t2 = _thread2;
//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
//关闭线程池
pool.shutdown();
while(true){
if(pool.isTerminated()){
if(staticClassInt!=0){
System.out.println("所有的子线程都结束了!");
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<200000;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<200000;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();
}
}
}

程序运行结果:

所有的子线程都结束了!

i>>>>>400000

vi>>>>>257432

si>>>>>222170

autoInteger>>>>>400000

synClassInt>>>>>400000

lockInt>>>>>400000

staticClassInt>>>>>266997
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: