RT,不知道怎么判断?

解决方案 »

  1.   

    我估计想多了
    用Lock应该可以
    package pkg;import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;public class TestLock { private Lock lock = new ReentrantLock();
    /**
     * @param args
     */
    public static void main(String[] args) {
    final TestLock instance = new TestLock();
    Thread t1 = new Thread(new Runnable() {

    @Override
    public void run() {
    boolean tryLock = instance.lock.tryLock();

    if (tryLock) {
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println(1);
    instance.lock.unlock();
    } else {
    System.out.println(2);
    }

    }
    });
    // t1.start();

    Thread t2 = new Thread(new Runnable() {

    @Override
    public void run() {
    boolean tryLock = instance.lock.tryLock();

    if (tryLock) {
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println(3);
    instance.lock.unlock();
    } else {
    System.out.println(4);
    }
    }
    });
    t2.start();
    t1.start();
    // t2.start();
    }}
      

  2.   


    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import java.util.concurrent.ConcurrentHashMap;public class ObjectLockJudgement {    private static final ConcurrentHashMap<Object, Thread> relation = new ConcurrentHashMap<Object, Thread>();    /**
         * @param args
         */
        public static void main(String[] args) {
            final int threadNumber = 100;        List<Object> lockers = new ArrayList<Object>(threadNumber);
            for (int i = 0; i < threadNumber; i++) {
                final Object locker = new Object();
                lockers.add(locker);
                new ThreadImpl(locker);
            }        while (!relation.isEmpty()) {
                for (final Object locker : lockers) {
                    final Thread t = relation.get(locker);
                    if (t != null)
                        System.out.println("the locker Object:" + locker.hashCode()
                                + " is held by thread:" + t.getName());
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }    static class ThreadImpl extends Thread {
            private Object locker;
            private static final Random r = new Random();        public ThreadImpl(Object locker) {
                this.locker = locker;
                this.start();
            }        @Override
            public void run() {
                try {
                    synchronized (locker) {
                        relation.put(locker, Thread.currentThread());
                        try {
                            Thread.sleep(r.nextInt(10000));
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                } finally {
                    relation.remove(locker);
                }
            }
        }}以上代码是将locker和thread做关联,可以得到object 锁对应的thread。
    但是我不知道判断的目的是什么,因为ReentrantLock的try locker,可以应该大的你相同的目的,而且不需要判断。