java.util.concurrent---练习

线程锁

/**
 * 1.两个同步方法,两个线程,标准打印?//one two
 * 2.新增Thread.sleep给getOne?//one two
 * 3.新增普通方法,getThree?//three one two
 * 4.两个Number对象?//two one
 * 5.修改getOne为静态同步方法?//two one
 * 6.修改均为静态方法?//one two
 * 7.修改为两个Number 对象?//one two
 */

/**
 * 关键:非静态方法的锁默认是this,静态方法的锁默认是Class实例
 */
public class TestLock {

    public static void main(String[] args) {
        Number number = new Number();
        Number number2 = new Number();

        new Thread(new Runnable() {
            @Override
            public void run() {
                number.getOne();
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                number2.getTwo();
            }
        }).start();

        /*new Thread(new Runnable() {
            @Override
            public void run() {
                number.getThree();
            }
        }).start();*/

    }

}

class Number {

    public static synchronized void getOne() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }

    public static synchronized void getTwo() {
        System.out.println("two");
    }

//    public void getThree() {
//        System.out.println("Three");
//    }

}

生产者与消费者案例

public class TestProductAndConsumer {

    public static void main(String[] args) {
        Clerk clerk = new Clerk();
        new Thread(new Product(clerk), "生产者A").start();
        new Thread(new Consumer(clerk), "消费者B").start();
        new Thread(new Product(clerk), "生产者C").start();
        new Thread(new Consumer(clerk), "消费者D").start();
    }


}

class Clerk {

    private int product;

    private Lock lock = new ReentrantLock();

    Condition condition = lock.newCondition();


    public void get() throws InterruptedException {

        lock.lock();

        try {
            while(product >= 1) {
                System.out.println("商品已满!");
                condition.await();
            }
            //Thread.sleep(500);
            System.out.println(Thread.currentThread().getName() + "进货: " + (++product));
            condition.signalAll();
        } finally {
            lock.unlock();
        }

    }

    public void sale() throws InterruptedException {

        lock.lock();

        try {
            while(product <= 0) {
                System.out.println("缺货!");
                condition.await();
            }
            //Thread.sleep(500);
            System.out.println(Thread.currentThread().getName() + "卖货: " + (--product));
            condition.signalAll();
        } finally {
            lock.unlock();
        }

    }

}

class Product implements Runnable {

    private Clerk clerk;

    public Product(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {

        for(int i = 0; i < 20; i++) {

            try {
                clerk.get();
            } catch (Exception e) {
            }

        }

    }
}

class Consumer implements Runnable{

    private Clerk clerk;

    public Consumer(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {

        for(int i = 0; i < 20; i++) {

            try {
                clerk.sale();
            } catch (Exception e) {
            }

        }

    }
}

循环打印ABC

public class TestAlternate {

    public static void main(String[] args) {

        Loop loop = new Loop();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i <= 20; i++) {
                    loop.loopA(i);
                }
            }
        }, "A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i <= 20; i++) {
                    loop.loopB(i);
                }
            }
        }, "B").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i <= 20; i++) {
                    loop.loopC(i);
                    System.out.println("-----------------------");
                }
            }
        }, "C").start();

    }



}

class Loop {

    private int number = 1;

    private Lock lock = new ReentrantLock();

    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();

    public void loopA(int total) {

        lock.lock();

        try {
            if(number != 1) {
                condition1.await();
            }
            for(int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i + "\t" + total);
            }
            number = 2;
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    public void loopB(int total) {

        lock.lock();

        try {
            if(number != 2) {
                condition2.await();
            }
            for(int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i + "\t" + total);
            }
            number = 3;
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    public void loopC(int total) {

        lock.lock();

        try {
            if(number != 3) {
                condition3.await();
            }
            for(int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i + "\t" + total);
            }
            number = 1;
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
}

你可能感兴趣的:(java.util.concurrent---练习)