commit 33ea7df7238227c4cd0b7efb136eec0b74494efa Author: bieshaojun <2958505310@qq.com> Date: Tue Dec 3 11:47:04 2024 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..63574ec --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/git_toolbox_blame.xml b/.idea/git_toolbox_blame.xml new file mode 100644 index 0000000..7dc1249 --- /dev/null +++ b/.idea/git_toolbox_blame.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..132404b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..236739a --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + test + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + \ No newline at end of file diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java new file mode 100644 index 0000000..6560c2b --- /dev/null +++ b/src/main/java/org/example/Main.java @@ -0,0 +1,88 @@ +package org.example; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicInteger; + +public class Main { + public static void main(String[] args) { + Queue pool = new ConcurrentLinkedQueue<>(); + for (int i = 0; i < 400; i++) { + pool.add(i); + } + CountDownLatch latch = new CountDownLatch(10000); + Semaphore semaphore = new Semaphore(400); + Map map = new HashMap<>(); + Map map2 = new HashMap<>(); + for (int i = 0; i < 400; i++) { + map.put(i, getNum(i)); + map2.put(i, new AtomicInteger(0)); + } + for (int i = 0; i < 10000; i++) { + try { + semaphore.acquire(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + new Thread(new Runnable() { + public void run() { + int count = 0, over = 0; + while (count++ < 400) { + Integer poll = pool.poll(); +// if (poll == null) System.out.println("null"); +// synchronized (Objects.requireNonNull(poll)) { + if (checkNum(poll)) { +// System.out.println(Thread.currentThread().getName() + ": "+poll); + pool.add(poll); + latch.countDown(); + map2.get(poll).incrementAndGet(); + over = 1; + break; + } else { + pool.add(poll); +// } + } + } + if (over == 0) { + System.out.println("over"); + latch.countDown(); + } + semaphore.release(); + } + + private boolean checkNum(Integer poll) { + try { + Thread.sleep(2000); + if (map2.get(poll).get() < map.get(poll)) { + return true; + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return false; + } + }).start(); + } + try { + latch.await(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + System.out.println("Hello world!"); + System.out.println(map2); + System.out.println(map); + } + + private static Integer getNum(int i) { + if (i % 2 == 0) { + return 40; + } else { + return 10; + } + } +} \ No newline at end of file