Skip to content

Commit f3ec438

Browse files
author
Jossc
committed
添加部分代码
1 parent 37631cf commit f3ec438

20 files changed

Lines changed: 282 additions & 12 deletions

File tree

pom.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,17 @@
4949
<artifactId>jedis</artifactId>
5050
<version>2.9.0</version>
5151
</dependency>
52-
53-
52+
<dependency>
53+
<groupId>junit</groupId>
54+
<artifactId>junit</artifactId>
55+
<version>RELEASE</version>
56+
<scope>compile</scope>
57+
</dependency>
58+
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
59+
<dependency>
60+
<groupId>mysql</groupId>
61+
<artifactId>mysql-connector-java</artifactId>
62+
<version>5.1.47</version>
63+
</dependency>
5464
</dependencies>
5565
</project>

src/main/java/com/basics/AtomicLongTest/AtomicLongTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void main(String []args) throws InterruptedException {
4040
oneThread.join();
4141
twoThread.join();
4242

43-
System.err.println("count : " + atomicLong.get());
43+
System.err.println("basicsThread : " + atomicLong.get());
4444

4545
}
4646
}

src/main/java/com/basics/ClassLoaderTest/ExtClassLoader/ExtClassLoaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public File[] test(){
3434
StringTokenizer stringTokenizer
3535
= new StringTokenizer(s,File.pathSeparator);
3636
int count = stringTokenizer.countTokens();
37-
System.out.println("count :" + count);
37+
System.out.println("basicsThread :" + count);
3838
dirs = new File[count];
3939
for (int i = 0; i < count; i++) {
4040
dirs[i] = new File(stringTokenizer.nextToken());

src/main/java/com/basics/ClassLoaderTest/SpiTest/package-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
* 所以Bootstrap 不能托付给AppClassLoader来加载所以需要打破。
88
* 托付给ContextClassLoader来加载
99
*
10+
*
1011
*/
1112
package com.basics.ClassLoaderTest.SpiTest;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.basics.Collection.ThrteadList;
2+
3+
import org.junit.runner.notification.RunListener;
4+
5+
import java.util.List;
6+
import java.util.concurrent.locks.Lock;
7+
import java.util.concurrent.locks.ReentrantLock;
8+
9+
10+
/**
11+
* @ClassName ThreadList
12+
* @Description TODO
13+
* @Author chenzhuo
14+
* @Date 2018/10/28 21:17
15+
* @Version 1.0
16+
* 线程安全的
17+
**/
18+
@RunListener.ThreadSafe
19+
public class ThreadList<T> {
20+
21+
private final List<T> list;
22+
23+
public ThreadList(List<T> list) {
24+
this.list = list;
25+
}
26+
27+
/**
28+
* 如果为空就添加
29+
* @param x
30+
* @return
31+
*/
32+
public synchronized boolean putIfAbsent(T x){
33+
boolean count = list.contains(x);
34+
if(count){
35+
list.add(x);
36+
}
37+
return !count;
38+
}
39+
public boolean putAbsent(T objec){
40+
Lock lock = new ReentrantLock();
41+
lock.lock();
42+
try {
43+
boolean count = list.contains(objec);
44+
if(count){
45+
list.add(objec);
46+
}
47+
return !count;
48+
}finally {
49+
lock.unlock();
50+
}
51+
}
52+
}

src/main/java/com/basics/JucTest/LockFree/CountDownLatchTestAndJoin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* countDownLatch.countDown()执行的时候减1
1414
* releases 传入的count值,
1515
* protected boolean tryReleaseShared(int releases) {
16-
* // Decrement count; signal when transition to zero
16+
* // Decrement basicsThread; signal when transition to zero
1717
* for (;;) {
1818
* //自旋获取state的值,就是new CountDownLatch(3)
1919
* //传入的值, 如果=0 直接返回false,不是的话就减1
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.basics.JucTest.basicsThread;
2+
3+
import java.util.concurrent.FutureTask;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* @ClassName Preloader
8+
* @Description TODO
9+
* @Author chenzhuo
10+
* @Date 2018/10/29 22:06
11+
* @Version 1.0
12+
**/
13+
public class Preloader {
14+
/* public final FutureTask<PreloaderInfo>
15+
futureTask = new FutureTask<PreloaderInfo>(()->{
16+
new PreloaderInfo();
17+
});*/
18+
19+
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.basics.JucTest.basicsThread;
2+
3+
import java.util.concurrent.locks.ReentrantLock;
4+
5+
/**
6+
* @ClassName PreloaderInfo
7+
* @Description TODO
8+
* @Author chenzhuo
9+
* @Date 2018/10/29 22:06
10+
* @Version 1.0
11+
**/
12+
public class PreloaderInfo {
13+
14+
15+
public static void main(String[] args) {
16+
ReentrantLock lock =new ReentrantLock();
17+
}
18+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.basics.JucTest.basicsThread;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
/**
6+
* @ClassName TestHarness
7+
* @Description TODO
8+
* @Author chenzhuo
9+
* @Date 2018/10/29 21:42
10+
* @Version 1.0
11+
**/
12+
public class TestHarness {
13+
14+
public long timeTasks(int nThreads,final Runnable task) throws InterruptedException {
15+
16+
final CountDownLatch start
17+
= new CountDownLatch(1);
18+
final CountDownLatch end
19+
= new CountDownLatch(nThreads);
20+
for(int i=0;i<nThreads;i++){
21+
Thread t = new Thread(()->{
22+
try {
23+
task.run();
24+
}finally {
25+
end.countDown();
26+
}
27+
/* try {
28+
start.await();
29+
try {
30+
task.run();
31+
}finally {
32+
end.countDown();
33+
}
34+
35+
} catch (InterruptedException e) {
36+
e.printStackTrace();
37+
}*/
38+
});
39+
t.start();
40+
}
41+
long startTime = System.nanoTime();
42+
// start.countDown();
43+
end.await();
44+
long endTime = System.nanoTime();
45+
return endTime -startTime;
46+
}
47+
48+
public static void main(String[] args) throws InterruptedException {
49+
TestHarness testHarness
50+
= new TestHarness();
51+
52+
Thread thread
53+
= new Thread(()->{
54+
for(int i=0;i<10;i++){
55+
System.out.println(i);
56+
};
57+
});
58+
testHarness.timeTasks(10,thread);
59+
60+
}
61+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.basics.JucTest.basicsThread;
2+
3+
import java.util.HashMap;
4+
import java.util.UUID;
5+
import java.util.concurrent.ConcurrentHashMap;
6+
7+
/**
8+
* @ClassName TestHashMap
9+
* @Description TODO
10+
* @Author chenzhuo
11+
* @Date 2018/10/23 22:27
12+
* @Version 1.0
13+
**/
14+
public class TestHashMap {
15+
public static void main(String[] args) throws InterruptedException {
16+
final HashMap<String,String>
17+
map = new HashMap<>(2);
18+
Thread t = new Thread(() -> {
19+
for (int i = 0; i < 10000; i++) {
20+
new Thread(() -> map.put(UUID.randomUUID().toString(), ""), "fff" + i).start();
21+
}
22+
}, "fff天团");
23+
t.start();
24+
t.join();
25+
26+
ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();
27+
28+
29+
30+
}
31+
}

0 commit comments

Comments
 (0)