1.按顺序打印ABC

三个线程,每个线程分别打印A,B,C各十次,现在要求按顺序输出A,B,C

package concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class 按顺序打印ABC { public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(3);
Object lockA = new Object();
Object lockB = new Object();
Object lockC = new Object();
Runnable rA = new OrderPrintABC(lockC, lockA, "A");
Runnable rB = new OrderPrintABC(lockA, lockB, "B");
Runnable rC = new OrderPrintABC(lockB, lockC, "C");
threadPool.execute(rA);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadPool.execute(rB);
threadPool.execute(rC);
threadPool.shutdown();
} private static class OrderPrintABC implements Runnable{ private Object preLock;
private Object selfLock;
private String taskName;
private int count = 10; public OrderPrintABC(Object preLock, Object selfLock, String taskName) {
this.preLock = preLock;
this.selfLock = selfLock;
this.taskName = taskName;
} @Override
public void run() {
while(count > 0) {
synchronized(preLock) {
synchronized(selfLock) {
System.out.print(taskName);
count--;
selfLock.notifyAll();
}
if(count > 0) {
try {
preLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}

2. 生产者消费者: 要尽量使用BlockingQueue来实现

https://github.com/CyC2018/CS-Notes/blob/master/notes/Java%20%E5%B9%B6%E5%8F%91.md#blockingqueue

package concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; public class 生产者消费者 { //有界队列
private static class BoundQueue<T> {
private Object[] items;
private int count = 0;
private Lock lock = new ReentrantLock();
private Condition notEmpty = lock.newCondition();
private Condition notFull = lock.newCondition(); public BoundQueue(int size) {
items = new Object[size];
} public void add(T t) throws InterruptedException {
lock.lock();
try {
Long nano = 0L;
while (count == items.length) {
if(nano < 0L) {
return;
};
System.out.println("仓库已满");
notFull.awaitNanos(5000);
}
items[count++] = t;
System.out.println("生产者放入" + t);
notEmpty.signal();
Thread.sleep(1000);
} finally {
lock.unlock();
}
} public T remove() throws InterruptedException {
lock.lock();
T t;
try {
Long nano = 0L;
while (count == 0) {
if(nano < 0L) {
return null;
};
System.out.println("仓库已空");
nano = notEmpty.awaitNanos(5000);
}
t = (T) items[count - 1];
items[--count] = null;
System.out.println("消费者接收" + t);
notFull.signal();
Thread.sleep(1000);
} finally {
lock.unlock();
}
return t;
}
} //生产者
private static class Product implements Runnable{
private BoundQueue queue;
private int count; public Product (BoundQueue queue, int count) {
this.queue = queue;
this.count = count;
} public void run() {
int itr = 1;
while(itr <= count) {
try {
queue.add(itr);
itr++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} //消费者
private static class Consumer implements Runnable{
private BoundQueue queue;
private int count; public Consumer(BoundQueue queue, int count) {
this.queue = queue;
this.count = count;
} @Override
public void run() {
int itr = 0;
while(itr <= count) {
try {
queue.remove();
itr++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} public static void main(String[] args) {
BoundQueue<Integer> queue = new BoundQueue<>(5);
int count = 6;
Runnable rP = new Product(queue, count);
Runnable rC = new Consumer(queue, count);
ExecutorService threadPool = Executors.newFixedThreadPool(2);
threadPool.execute(rP);
threadPool.execute(rC);
threadPool.shutdown();
}
}

https://github.com/CyC2018/CS-Notes/blob/master/notes/Java%20%E5%B9%B6%E5%8F%91.md#blockingqueue

Java并发练习的更多相关文章

  1. 多线程的通信和同步(Java并发编程的艺术--笔记)

    1. 线程间的通信机制 线程之间通信机制有两种: 共享内存.消息传递.   2. Java并发 Java的并发采用的是共享内存模型,Java线程之间的通信总是隐式执行,通信的过程对于程序员来说是完全透 ...

  2. 【Java并发编程实战】----- AQS(四):CLH同步队列

    在[Java并发编程实战]-–"J.U.C":CLH队列锁提过,AQS里面的CLH队列是CLH同步锁的一种变形.其主要从两方面进行了改造:节点的结构与节点等待机制.在结构上引入了头 ...

  3. 【Java并发编程实战】----- AQS(三):阻塞、唤醒:LockSupport

    在上篇博客([Java并发编程实战]----- AQS(二):获取锁.释放锁)中提到,当一个线程加入到CLH队列中时,如果不是头节点是需要判断该节点是否需要挂起:在释放锁后,需要唤醒该线程的继任节点 ...

  4. 【Java并发编程实战】----- AQS(二):获取锁、释放锁

    上篇博客稍微介绍了一下AQS,下面我们来关注下AQS的所获取和锁释放. AQS锁获取 AQS包含如下几个方法: acquire(int arg):以独占模式获取对象,忽略中断. acquireInte ...

  5. 【Java并发编程实战】-----“J.U.C”:CLH队列锁

    在前面介绍的几篇博客中总是提到CLH队列,在AQS中CLH队列是维护一组线程的严格按照FIFO的队列.他能够确保无饥饿,严格的先来先服务的公平性.下图是CLH队列节点的示意图: 在CLH队列的节点QN ...

  6. 【Java并发编程实战】-----“J.U.C”:CountDownlatch

    上篇博文([Java并发编程实战]-----"J.U.C":CyclicBarrier)LZ介绍了CyclicBarrier.CyclicBarrier所描述的是"允许一 ...

  7. 【Java并发编程实战】-----“J.U.C”:CyclicBarrier

    在上篇博客([Java并发编程实战]-----"J.U.C":Semaphore)中,LZ介绍了Semaphore,下面LZ介绍CyclicBarrier.在JDK API中是这么 ...

  8. 【Java并发编程实战】-----“J.U.C”:ReentrantReadWriteLock

    ReentrantLock实现了标准的互斥操作,也就是说在某一时刻只有有一个线程持有锁.ReentrantLock采用这种独占的保守锁直接,在一定程度上减低了吞吐量.在这种情况下任何的"读/ ...

  9. Java并发编程:volatile关键字解析

    Java并发编程:volatile关键字解析 volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在 ...

  10. JAVA并发编程J.U.C学习总结

    前言 学习了一段时间J.U.C,打算做个小结,个人感觉总结还是非常重要,要不然总感觉知识点零零散散的. 有错误也欢迎指正,大家共同进步: 另外,转载请注明链接,写篇文章不容易啊,http://www. ...

随机推荐

  1. Eclipse中,No compiler is provided in this environment. Perhaps you are running on a JRE rather than a

    问题说明 Eclipse导入Maven项目后,执行 mvn clean install后,出现如下错误: [INFO] ---------------------------------------- ...

  2. 【kinetic】操作系统探索总结(五)创建简单的机器人模型smartcar

    p { margin-bottom: 0.1in; direction: ltr; line-height: 120%; text-align: justify } a:link { color: r ...

  3. Docker下配置KeepAlive支持nginx高可用

    案例子任务一.安装配置keepalived 步骤1:使用nginx镜像生成nginx-keep镜像 1) 启动nginx容器并进入 docker run -d --privileged nginx / ...

  4. Java优雅停机

    Java的优雅停机通常通过注册JDK的ShootDownHook实现,当系统接受到退出指令后,首先标记系统处于退出状态,不再接受新的消息,然后将积压的消息处理完,最后调用资源回收接口将资源销毁,最后各 ...

  5. js 的关键字

    1.get / set var test = { _Name: "Limei", _Age: 20, get name() { return this._Name;}, set a ...

  6. 关于Objects类的getClass方法为什么可以得到子类的地址的思考

    这一段时间,总是很纠结为什么Objects中的getClass方法可以返回包含子类地址信息的东西(我不确定返回值类型). 因为在Java中,我们定义的父类,我想破脑袋也想不出怎么可以得到子类的信息. ...

  7. Java基础--接口回调(接口 对象名 = new 类名)理解

    接口 对象名1 = new 类名和类名 对象名2 = new 类名的区别是什么? 实例 /** *Person.java 接口 */ public interface Person { void in ...

  8. 利用github给国外文件下载加速

    前言 作为一名程序员,经常需要下载一些编程相关的环境,而国内的网络环境大家都知道,有的文件用浏览器是下载不动的,于是我有了利用github下载文件的想法. 我的demo项目地址:https://git ...

  9. LeetCode876 链表的中间结点

    给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. 示例 1: 输入:[1,2,3,4,5] 输出:此列表中的结点 3 (序列化形式:[3,4 ...

  10. oracle出现未选定行

    初学oracle,在SQLplus输入查询命令 出现了以下情况.. 后来了解到oracle的SQL语句其中有些词必须大写才会有效. 在这个语句中将username后面的值改为大写就可以了. 还有一种就 ...