import java.util.concurrent.ConcurrentLinkedDeque;
public class AddTask implements Runnable {
private ConcurrentLinkedDeque<String> list;
public AddTask(ConcurrentLinkedDeque<String> list) {
this.list = list;
}
@Override
public void run() {
String name = Thread.currentThread().getName();
for(int i = 0; i < 10000; i++){
list.add(name+":Element "+i);
}
}
}
import java.util.concurrent.ConcurrentLinkedDeque;
public class PollTask implements Runnable {
private ConcurrentLinkedDeque<String> list;
public PollTask(ConcurrentLinkedDeque<String> list) {
this.list = list;
}
@Override
public void run() {
for(int i = 0; i < 5000; i ++) {
/*String ele = list.pollFirst();
if (ele == null) {
System.out.println("取出元素为空");
}
ele = list.pollLast();
if (ele == null) {
System.out.println("取出元素为空");
}*/
System.out.println("取出:"+list.pollFirst());
System.out.println("取出:"+list.pollLast());
}
}
}
import java.util.concurrent.ConcurrentLinkedDeque;
public class Main {
public static void main(String[] args) {
// 共享集合
ConcurrentLinkedDeque<String> list = new ConcurrentLinkedDeque<String>();
// 执行集合元素添加任务
Thread[] threads = new Thread[100];
for (int i = 0; i < threads.length; i++) {
AddTask addTask = new AddTask(list);
threads[i] = new Thread(addTask);
threads[i].start();
}
// 等待线程完成
for (int i = 0; i < threads.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 打印添加任务之后共享集合的元素大小
System.out.println("Size of the List:" + list.size());
// 执行集合取出任务
for (int i = 0; i < threads.length; i++) {
PollTask pollTask = new PollTask(list);
threads[i] = new Thread(pollTask);
threads[i].start();
}
// 等待线程完成
for (int i = 0; i < threads.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 打印添加任务之后共享集合的元素大小
System.out.println("Size of the List:" + list.size());
}
}
import java.util.concurrent.ConcurrentLinkedDeque;
/**
* 可以同时并发执行读写操作
* @author liujun
*
*/
public class Main2 {
public static void main(String[] args) {
// 共享集合
ConcurrentLinkedDeque<String> list = new ConcurrentLinkedDeque<String>();
// 执行集合元素添加任务
Thread[] threads = new Thread[100];
for (int i = 0; i < threads.length; i++) {
AddTask addTask = new AddTask(list);
threads[i] = new Thread(addTask);
threads[i].start();
}
Thread[] threads2 = new Thread[100];
// 执行集合取出任务
for (int i = 0; i < threads2.length; i++) {
PollTask pollTask = new PollTask(list);
threads2[i] = new Thread(pollTask);
threads2[i].start();
}
System.out.println("---------------");
}
}
import java.util.concurrent.ConcurrentLinkedDeque;
public class AddTask implements Runnable {
private ConcurrentLinkedDeque<String> list;
public AddTask(ConcurrentLinkedDeque<String> list) {
this.list = list;
}
@Override
public void run() {
String name = Thread.currentThread().getName();
for(int i = 0; i < 10000; i++){
list.add(name+":Element "+i);
}
}
}
import java.util.concurrent.ConcurrentLinkedDeque;
public class PollTask implements Runnable {
private ConcurrentLinkedDeque<String> list;
public PollTask(ConcurrentLinkedDeque<String> list) {
this.list = list;
}
@Override
public void run() {
for(int i = 0; i < 5000; i ++) {
/*String ele = list.pollFirst();
if (ele == null) {
System.out.println("取出元素为空");
}
ele = list.pollLast();
if (ele == null) {
System.out.println("取出元素为空");
}*/
System.out.println("取出:"+list.pollFirst());
System.out.println("取出:"+list.pollLast());
}
}
}
import java.util.concurrent.ConcurrentLinkedDeque;
public class Main {
public static void main(String[] args) {
// 共享集合
ConcurrentLinkedDeque<String> list = new ConcurrentLinkedDeque<String>();
// 执行集合元素添加任务
Thread[] threads = new Thread[100];
for (int i = 0; i < threads.length; i++) {
AddTask addTask = new AddTask(list);
threads[i] = new Thread(addTask);
threads[i].start();
}
// 等待线程完成
for (int i = 0; i < threads.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 打印添加任务之后共享集合的元素大小
System.out.println("Size of the List:" + list.size());
// 执行集合取出任务
for (int i = 0; i < threads.length; i++) {
PollTask pollTask = new PollTask(list);
threads[i] = new Thread(pollTask);
threads[i].start();
}
// 等待线程完成
for (int i = 0; i < threads.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 打印添加任务之后共享集合的元素大小
System.out.println("Size of the List:" + list.size());
}
}
import java.util.concurrent.ConcurrentLinkedDeque;
/**
* 可以同时并发执行读写操作
* @author liujun
*
*/
public class Main2 {
public static void main(String[] args) {
// 共享集合
ConcurrentLinkedDeque<String> list = new ConcurrentLinkedDeque<String>();
// 执行集合元素添加任务
Thread[] threads = new Thread[100];
for (int i = 0; i < threads.length; i++) {
AddTask addTask = new AddTask(list);
threads[i] = new Thread(addTask);
threads[i].start();
}
Thread[] threads2 = new Thread[100];
// 执行集合取出任务
for (int i = 0; i < threads2.length; i++) {
PollTask pollTask = new PollTask(list);
threads2[i] = new Thread(pollTask);
threads2[i].start();
}
System.out.println("---------------");
}
}