commit
33ea7df723
@ -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 |
@ -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 |
@ -0,0 +1,8 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project version="4"> |
||||
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8"> |
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" /> |
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" /> |
||||
<file url="PROJECT" charset="UTF-8" /> |
||||
</component> |
||||
</project> |
@ -0,0 +1,6 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project version="4"> |
||||
<component name="GitToolBoxBlameSettings"> |
||||
<option name="version" value="2" /> |
||||
</component> |
||||
</project> |
@ -0,0 +1,14 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project version="4"> |
||||
<component name="ExternalStorageConfigurationManager" enabled="true" /> |
||||
<component name="MavenProjectsManager"> |
||||
<option name="originalFiles"> |
||||
<list> |
||||
<option value="$PROJECT_DIR$/pom.xml" /> |
||||
</list> |
||||
</option> |
||||
</component> |
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> |
||||
<output url="file://$PROJECT_DIR$/out" /> |
||||
</component> |
||||
</project> |
@ -0,0 +1,17 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
|
||||
<groupId>org.example</groupId> |
||||
<artifactId>test</artifactId> |
||||
<version>1.0-SNAPSHOT</version> |
||||
|
||||
<properties> |
||||
<maven.compiler.source>8</maven.compiler.source> |
||||
<maven.compiler.target>8</maven.compiler.target> |
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
||||
</properties> |
||||
|
||||
</project> |
@ -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<Integer> pool = new ConcurrentLinkedQueue<>(); |
||||
for (int i = 0; i < 400; i++) { |
||||
pool.add(i); |
||||
} |
||||
CountDownLatch latch = new CountDownLatch(10000); |
||||
Semaphore semaphore = new Semaphore(400); |
||||
Map<Integer, Integer> map = new HashMap<>(); |
||||
Map<Integer, AtomicInteger> 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; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue