[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[整理]java中线程同步,ReentrantLock和synchronized的一点区别。

上一篇:[整理]Java中ThreadLocal的理解
下一篇:[整理]Java中,Log4J的log4j.properties的写法

添加日期:2012/5/28 15:22:29 快速返回   返回列表 阅读4602次
写个小类,希望同时只有一个任务在执行,其它任务来之后,直接返回false。

首先尝试了synchronized,代码如下:


package thread;

import java.util.Random;

public class CmdRunnerSynchronized {

    public static boolean IS_RUNNING = false;

    private static final Object lock = new Object();

    public boolean start() {

        // 如果有任务在执行,直接返回
        if (IS_RUNNING) {
            System.out.println("--is running---"
                    + Thread.currentThread().getName());
            return false;
        }

        // sleep一会
        try {
            Thread.sleep(new Random().nextInt(2000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        synchronized (lock) {
            IS_RUNNING = true;
            System.out.println("--run start---"
                    + Thread.currentThread().getName());

            // sleep一会
            try {
                Thread.sleep(new Random().nextInt(2000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("--run end---"
                    + Thread.currentThread().getName());
            IS_RUNNING = false;
        }

        return true;
    }

    public static void main(String[] args) {

        // 开10个线程
        for (int i = 0; i < 10; i++) {
            new Thread("Thread" + i) {
                public void run() {
                    try {
                        Thread.sleep(new Random().nextInt(1000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    CmdRunnerSynchronized run = new CmdRunnerSynchronized();
                    run.start();
                }
            }.start();
        }
    }
}


运行结果如下:
------------------------------
--run start---Thread0
--is running---Thread6
--run end---Thread0
--run start---Thread9
--is running---Thread4
--is running---Thread8
--is running---Thread3
--run end---Thread9
--run start---Thread2
--run end---Thread2
--run start---Thread7
--run end---Thread7
--run start---Thread1
--run end---Thread1
--run start---Thread5
--run end---Thread5
------------------------------
只有3,4,6,8直接返回了,其它都等待,直到被执行。
这是因为开始的IS_RUNNING判断,这些线程都通过了,然后在synchronized处等待。
这样不符合要求。

然后想到了传说中的ReentrantLock,它有个tryLock()方法,
取到锁的话,返回true,没取到的话,马上返回false,不等待的~~
大概用法如下:
------------------
   Lock lock = ...;
      if (lock.tryLock()) {
          try {
              // manipulate protected state
          } finally {
              lock.unlock();
          }
      } else {
          // perform alternative actions
      }
------------------
代码改改如下:


package thread;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class CmdRunner {

    public static boolean IS_RUNNING = false;

    private static Lock scoreLock = new ReentrantLock();

    public boolean start() {

        if (scoreLock.tryLock()) {
            try {

                // sleep一会
                try {
                    Thread.sleep(new Random().nextInt(2000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                IS_RUNNING = true;
                System.out.println("--run start---"
                        + Thread.currentThread().getName());

                // sleep一会
                try {
                    Thread.sleep(new Random().nextInt(2000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("--run end---"
                        + Thread.currentThread().getName());
                IS_RUNNING = false;
                return true;
            } finally {
                scoreLock.unlock();
            }
        } else {
            System.out.println("--is running---"
                    + Thread.currentThread().getName());
            return false;
        }

    }

    public static void main(String[] args) {

        // 开10个线程
        for (int i = 0; i < 10; i++) {
            new Thread("Thread" + i) {
                public void run() {
                    try {
                        Thread.sleep(new Random().nextInt(1000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    CmdRunner run = new CmdRunner();
                    run.start();
                }
            }.start();
        }
    }
}


运行结果如下:
-------------------
--is running---Thread8
--is running---Thread4
--is running---Thread9
--is running---Thread2
--is running---Thread5
--is running---Thread0
--is running---Thread7
--is running---Thread6
--run start---Thread1
--is running---Thread3
--run end---Thread1
----------------------
都是一个线程执行,其它直接返回。
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved