public class LinkedListTest {
public static class test {
AtomicInteger atomicInteger = new AtomicInteger();
LinkedList linkedList = new LinkedList();
LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue();
ArrayBlockingQueue arrayBlockingQueue=new ArrayBlockingQueue(5000000);
ConcurrentLinkedQueue concurrentLinkedQueue = new ConcurrentLinkedQueue();
DelayQueue<DelayTask> delayQueue=new DelayQueue<>();
PriorityBlockingQueue priorityBlockingQueue=new PriorityBlockingQueue();
public void link() {
linkedList.add(atomicInteger.getAndIncrement());
}
public void linkSize() {
System.out.println("link:" + linkedList.size());
System.out.println("linkReal:" + atomicInteger.toString());
}
public void linkBlock() throws InterruptedException {
linkedBlockingQueue.add(atomicInteger.getAndIncrement());
}
public void linkBlockSize() {
System.out.println("linkBlock:" + linkedBlockingQueue.size());
System.out.println("linkBlockReal:" + atomicInteger.toString());
}
public void concurrent() {
concurrentLinkedQueue.offer(atomicInteger.getAndIncrement());
}
public void concurrentSize() {
System.out.println("concurrent:" + concurrentLinkedQueue.size());
System.out.println("concurrentReal:" + atomicInteger);
}
public void arrayBlock(){
arrayBlockingQueue.add(atomicInteger.getAndIncrement());
}
public void arrayBlockSize(){
System.out.println("arrayBlock:"+arrayBlockingQueue.size());
System.out.println("arrayBlockReal:"+atomicInteger.toString());
}
public void priorityBlock(){
priorityBlockingQueue.add(atomicInteger.getAndIncrement());
}
public void priorityBlockSize(){
System.out.println("priorityBlock:"+priorityBlockingQueue.size());
System.out.println("priorityBlockReal:"+atomicInteger.toString());
}
public void delayQue(){
DelayTask delayTask=new DelayTask(atomicInteger.getAndIncrement(),"task"+atomicInteger.intValue());
delayQueue.add(delayTask);
}
public void delayQueSize() throws InterruptedException {
System.out.println("delayQue:"+delayQueue.size());
DelayTask take = delayQueue.take();
System.out.println("delayQueReal:"+atomicInteger.toString()+"__take:"+take.toString());
}
}
static class DelayTask implements Delayed{
private long delayTime;//该任务需要再队列中的延迟的时候
private long expire;//这个时间表示当前时间和延迟时间相加,这里就叫做到期时间
private String taskName;//任务的名称
@Override
public String toString() {
return "DelayTask{" +
"delayTime=" + delayTime +
", expire=" + expire +
", taskName='" + taskName + '\'' +
'}';
}
public DelayTask(long delayTime, String taskName) {
this.delayTime = delayTime;
this.taskName = taskName;
this.expire=System.currentTimeMillis()+delayTime;
}
@Override
public long getDelay(TimeUnit unit) {
return unit.convert(this.expire-System.currentTimeMillis(),TimeUnit.MILLISECONDS);
}
@Override
public int compareTo(Delayed o) {
return (int) (this.getDelay(TimeUnit.MILLISECONDS)-o.getDelay(TimeUnit.MILLISECONDS));
}
}
public static void test() throws InterruptedException {
final test l = new test();
final test l1 = new test();
final test l2 = new test();
final test l3=new test();
final test l4=new test();
final test l5=new test();
class linkedlist implements Runnable {
@Override
public void run() {
while (l.atomicInteger.intValue() < 2000000) {
l.link();
}
}
}
class linkBlockqueue implements Runnable {
@Override
public void run() {
try {
while (l1.atomicInteger.intValue() < 2000000) {
l1.linkBlock();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class concurrentQue implements Runnable {
@Override
public void run() {
while (l2.atomicInteger.intValue() < 2000000) {
l2.concurrent();
}
}
}
class arrayBlockQue implements Runnable{
@Override
public void run() {
while (l3.atomicInteger.intValue()<2000000){
l3.arrayBlock();
}
}
}
class priorityBlock implements Runnable{
@Override
public void run() {
while (l4.atomicInteger.intValue()<2000000){
l4.priorityBlock();
}
}
}
class delayQue implements Runnable{
@Override
public void run() {
while (l5.atomicInteger.intValue()<2000000){
l5.delayQue();
}
}
}
ExecutorService service = Executors.newCachedThreadPool();
ExecutorService service1 = Executors.newCachedThreadPool();
ExecutorService service2 = Executors.newCachedThreadPool();
ExecutorService service3 = Executors.newCachedThreadPool();
ExecutorService service4 = Executors.newCachedThreadPool();
ExecutorService service5 = Executors.newCachedThreadPool();
linkedlist linkedlist = new linkedlist();
linkBlockqueue blockqueue = new linkBlockqueue();
concurrentQue concurrentQue = new concurrentQue();
arrayBlockQue arrayBlockQue=new arrayBlockQue();
priorityBlock priorityBlock=new priorityBlock();
delayQue delayQue=new delayQue();
for (int i = 0; i < 20; i++) {
service.submit(linkedlist);
}
for (int i = 0; i < 20; i++) {
service1.submit(blockqueue);
}
for (int i = 0; i < 20; i++) {
service2.submit(concurrentQue);
}
for (int i=0;i<20;i++){
service3.submit(arrayBlockQue);
}
for (int i=0;i<20;i++){
service4.submit(priorityBlock);
}
for (int i = 0; i < 20; i++) {
service5.submit(delayQue);
}
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
}
service.shutdownNow();
service1.shutdownNow();
service2.shutdownNow();
service3.shutdownNow();
service4.shutdownNow();
service5.shutdownNow();
l.linkSize();
l1.linkBlockSize();
l2.concurrentSize();
l3.arrayBlockSize();
l4.priorityBlockSize();
l5.delayQueSize();
}
public static void main(String[] args) throws InterruptedException {
LinkedListTest.test();
}
}
增加减少线程数,可以看出LinkedList读多写少,出现安全问题