Java 读书笔记 21.2 并发

本文介绍了一个使用Java实现的并发编程案例,包括自定义的抽象类IntGenerator及其子类EvenGenerator和MutexEvenGenerator,演示了如何利用synchronized关键字和ReentrantLock进行线程同步,确保线程安全地生成偶数序列,并通过EvenChecker类验证生成的数字是否为偶数。

并发

public abstract class IntGenerator {
    private volatile boolean canceled = false;
    public abstract int next();
    // Allow this to be canceled:
    public void cancel() { canceled = true; }
    public boolean isCanceled() { return canceled; }
}
public class EvenChecker implements Runnable {
    private IntGenerator generator;
    private final int id;
    public EvenChecker(IntGenerator g, int ident) {
        generator = g;
        id = ident;
    }
    public void run() {
        while(!generator.isCanceled()) {
            int val = generator.next();
            //System.out.println(val);
            if(val % 2 != 0) {
                System.out.println(val + " not even!");
                generator.cancel(); // Cancels all EvenCheckers
            }
        }
    }
    // Test any type of IntGenerator:
    public static void test(IntGenerator gp, int count) {
        System.out.println("Press Control-C to exit");
        ExecutorService exec = Executors.newCachedThreadPool();
        for(int i = 0; i < count; i++)
            exec.execute(new EvenChecker(gp, i));
        exec.shutdown();
    }
    // Default value for count:
    public static void test(IntGenerator gp) {
        test(gp, 10);
    }
} 
public class EvenGenerator extends IntGenerator {
    private int currentEvenValue = 0;
    public  synchronized  int next() {
        ++currentEvenValue;    //简单地说synchronized关键字将next()锁了起来,
                              //加两次的中间是不会被的线程抢走;
        Thread.yield();
        ++currentEvenValue;
        return currentEvenValue;
    }
    public static void main(String[] args) {
        EvenChecker.test(new EvenGenerator());

    }
}

或者通过显式的lock关键字


public class MutexEvenGenerator extends IntGenerator {  
  private int currentEvenValue = 0;  
  private Lock lock = new ReentrantLock();  
  public int next() {  
    lock.lock();  
    try {  
      ++currentEvenValue;  
      Thread.yield(); // Cause failure faster  
      ++currentEvenValue;  
      return currentEvenValue;  
    } finally {  
      lock.unlock();  
    }  
  }  
  public static void main(String[] args) {  
    EvenChecker.test(new MutexEvenGenerator());  
  }  
} 

你可能发现lock要写好多东西,这也是java的思想,宁愿写的多,也舒服,非常明显的锁和释放,而且更细

import java.util.concurrent.*;  
import java.util.concurrent.locks.*;  

public class AttemptLocking {  
  private ReentrantLock lock = new ReentrantLock();  
  public void untimed() {  
    boolean captured = lock.tryLock();  
    try {  
      System.out.println("tryLock(): " + captured);  
    } finally {  
      if(captured)  
        lock.unlock();  
    }  
  }  
  public void timed() {  
    boolean captured = false;  
    try {  
      captured = lock.tryLock(2, TimeUnit.SECONDS);  
    } catch(InterruptedException e) {  
      throw new RuntimeException(e);  
    }  
    try {  
      System.out.println("tryLock(2, TimeUnit.SECONDS): " +  
        captured);  
    } finally {  
      if(captured)  
        lock.unlock();  
    }  
  }  
  public static void main(String[] args) {  
    final AttemptLocking al = new AttemptLocking();  
    al.untimed(); // True -- lock is available  
    al.timed();   // True -- lock is available  
    // Now create a separate task to grab the lock:  
    new Thread() {  
      { setDaemon(true); }  
      public void run() {  
        al.lock.lock();  
        System.out.println("acquired");  
      }  
    }.start();  
    Thread.yield(); // Give the 2nd task a chance  
    al.untimed(); // False -- lock grabbed by task  
    al.timed();   // False -- lock grabbed by task  
  }  

当线程被锁起来,下面两个函数虽然拿不到锁,但是还可以做别的;

原子性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值