JUC与JVM(3)线程之间精确调度

JUC与JVM 线程通信之JDK1.8Lock锁 线程间隔调度

题目:

利用多线程编程,实现依次,A打印5次,B打印10次,C打印15次;至此为一轮,要求共进行10轮;

背景知识:link.

private Condition condition = lock.newCondition();
condition.await();
condition.signal();
condition.signalAll();

这个线程对象的使用和理解

Condition.awaite();

Condition对象和Thread.waite()/Thread.notify()的异同;

  1. 调用Condition的await()和signal()方法,都必须在lock保护之内,就是说必须在lock.lock()和lock.unlock之间才可以使用;

  2. Conditon中的await()对应Object的wait();

    Condition中的signal()对应Object的notify();

    Condition中的signalAll()对应Object的notifyAll()。

3.因Lock是通过创建对象的机制来实现生产者和消费者的相互调度,故可以实现多锁机制;详情请查看:link.

代码如下:

public class ShareData {
    private Lock lock = new ReentrantLock();
    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();
    private int number = 1;

    public void print5() {
        lock.lock();
        try {
            while (number != 1)
                c1.await();
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + "  " + (i + 1));
            }
            number = 2;
            c2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void print10() {
        lock.lock();
        try {
            while (number != 2)
                c2.await();
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "  " + (i + 1));
            }
            number = 3;
            c3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void print15() {
        lock.lock();
        try {
            while (number != 3)
                c3.await();
            for (int i = 0; i < 15; i++) {
                System.out.println(Thread.currentThread().getName() + "  " + (i + 1));
            }
            number = 1;
            c1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

class Test {
    public static void main(String[] args) {
        ShareData sd = new ShareData();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                sd.print5();
            }
        }, "A").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                sd.print10();
            }
        }, "B").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                sd.print15();
            }
        }, "C").start();
    }

}

如此实现了各线程之间的精确调度;

问题:如何把print5,print10,print15 ,这三个方法,合三为一?

public class ShareData {
    private Lock lock = new ReentrantLock();
    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();
    private Condition[] cs = new Condition[4];
    private int number = 1;

    public ShareData() {
        cs[1] = c1;
        cs[2] = c2;
        cs[3] = c3;
    }

    public void print(int number) {
        lock.lock();
        try {
            Condition condition = cs[number];
            while (number!=this.number){
                condition.await();
            }
            for (int i = 0; i < number * 5; i++) {
                System.out.println(Thread.currentThread().getName() + "  " + (i + 1));
            }
            this.number = (this.number+1)>=4?1:++this.number;
            cs[this.number].signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}

class Test {
    public static void main(String[] args) {
        ShareData sd = new ShareData();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                sd.print(1);
            }
        }, "A").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                sd.print(2);
            }
        }, "B").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                sd.print(3);
            }
        }, "C").start();
    }

}

Callable接口

Callable和Runable的区别

  1. Callable泛型,并作为其方法call()。的返回值
  2. call方法有返回值,Runnable的run()方法无返回值;
  3. call方法,有异常

如何利用Callable接口?

前提知识:如何重构下述代码中的两个方法?

public void m1(A a){}
public void m2(C c){}

即利用接口的特性新增接口B

class A implements B{}
class C implements B{}
//重构方法m1,m2
public void m3(B b){}

如此不够好,破坏了m1,m2,的可复用性;若A,C都为接口,亦可以

interface B implements A,C;
public void m3(B b){}

同时类型为B亦可以使用m1和m2方法;

Callable和Runnable的调用方式,则采用了上述方法;

Runnable --子–>RunnableFuture --子–>FutureTask–(constructor)–FutureTask(Callable<V> callable)

如此可以利用此关系树,完成对Callable中的call方法的调用;

使用详情:

class Test {
    public static void main(String[] args) throws Exception {
        FutureTask<String> futureTask = new FutureTask<>(new MyCallable());
        new Thread(futureTask,"A").start();
      //得到call()方法的返回值
        String s = futureTask.get();
    }
}
class MyCallable implements Callable<String>{

    @Override
    public String call() throws Exception {
        System.out.println("test ");
        return "success";
    }
}

JVM入门

JVM位置:
在这里插入图片描述

jvm是运行在操作系统之上的;它和硬件没有直接交互;

JVM架构体系:在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值