[Feature] add for new
This commit is contained in:
@@ -0,0 +1,59 @@
|
|||||||
|
package com.github.houbb.lock.test.lock;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author binbin.hou
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public class CowArraySetDemo {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读线程
|
||||||
|
*/
|
||||||
|
private static class ReadTask implements Runnable {
|
||||||
|
Set<String> set;
|
||||||
|
|
||||||
|
public ReadTask(Set<String> set) {
|
||||||
|
this.set = set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
System.out.println(set);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 写线程
|
||||||
|
*/
|
||||||
|
private static class WriteTask implements Runnable {
|
||||||
|
private Set<String> set;
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
public WriteTask(Set<String> set, String value) {
|
||||||
|
this.set = set;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
set.remove(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final int NUM = 5;
|
||||||
|
Set<String> set = new CopyOnWriteArraySet<>();
|
||||||
|
for (int i = 0; i < NUM; i++) {
|
||||||
|
set.add("main_" + i);
|
||||||
|
}
|
||||||
|
ExecutorService executorService = Executors.newFixedThreadPool(NUM);
|
||||||
|
for (int i = 0; i < NUM; i++) {
|
||||||
|
executorService.execute(new WriteTask(set, "main_" + i));
|
||||||
|
executorService.execute(new ReadTask(set));
|
||||||
|
}
|
||||||
|
executorService.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package com.github.houbb.lock.test.lock;
|
||||||
|
|
||||||
|
import java.util.concurrent.PriorityBlockingQueue;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author binbin.hou
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public class PriorityBlockingQueueDemo {
|
||||||
|
|
||||||
|
private static class User implements Comparable<User> {
|
||||||
|
|
||||||
|
private final int order;
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
private User(int order, String name) {
|
||||||
|
this.order = order;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(User o) {
|
||||||
|
return this.order - o.order;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User{" +
|
||||||
|
"order=" + order +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private PriorityBlockingQueue<User> queue = new PriorityBlockingQueue<>();
|
||||||
|
|
||||||
|
public void put(final User user) throws InterruptedException {
|
||||||
|
System.out.println("设置开始");
|
||||||
|
queue.put(user);
|
||||||
|
System.out.println("设置完成: " + user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void take() throws InterruptedException {
|
||||||
|
TimeUnit.SECONDS.sleep(1);
|
||||||
|
System.out.println("获取开始");
|
||||||
|
User take = queue.take();
|
||||||
|
System.out.println("获取成功: " + take);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final PriorityBlockingQueueDemo queueTest = new PriorityBlockingQueueDemo();
|
||||||
|
// 写入线程
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
for(int i = 0; i < 5; i++) {
|
||||||
|
int order = ThreadLocalRandom.current().nextInt(10);
|
||||||
|
User user = new User(order, i+"-user");
|
||||||
|
queueTest.put(user);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
// 读取线程
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
queueTest.take();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user