Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8b340f1

Browse files
更新线程池测试用例代码
1 parent ca059a5 commit 8b340f1

File tree

8 files changed

+361
-28
lines changed

8 files changed

+361
-28
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.javaedge.concurrency.common.closure;
2+
3+
import org.testng.annotations.Test;
4+
5+
6+
/**
7+
*
8+
* @author JavaEdge
9+
* @date 2019年10月9日
10+
*/
11+
public class ThreadClosure {
12+
13+
/**
14+
* threadLocal变量,每个线程都有一个副本,互不干扰
15+
*/
16+
public static ThreadLocal<String> value = new ThreadLocal<>();
17+
18+
@Test
19+
public void threadLocalTest() throws Exception {
20+
21+
// 主线程设值
22+
value.set("这是主线程设置的123");
23+
String v = value.get();
24+
System.out.println("线程1执行之前,主线程取到的值:" + v);
25+
26+
new Thread(() -> {
27+
String v1 = value.get();
28+
System.out.println("线程1取到的值:" + v1);
29+
// 设置 threadLocal
30+
value.set("这是线程1设置的456");
31+
32+
v1 = value.get();
33+
System.out.println("重新设置之后,线程1取到的值:" + v1);
34+
System.out.println("线程1执行结束");
35+
}).start();
36+
37+
// 等待所有线程执行结束
38+
Thread.sleep(5000L);
39+
40+
v = value.get();
41+
System.out.println("线程1执行之后,主线程取到的值:" + v);
42+
43+
}
44+
}
45+

‎concurrency/src/main/java/com/javaedge/concurrency/example/threadLocal/RequestHolder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.javaedge.concurrency.example.threadLocal;
22

3+
/**
4+
*
5+
* @author JavaEdge
6+
* @date 2019年10月9日
7+
*/
38
public class RequestHolder {
49

510
private final static ThreadLocal<Long> requestHolder = new ThreadLocal<>();

‎concurrency/src/main/java/com/javaedge/concurrency/example/threadLocal/ThreadLocalController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
import org.springframework.web.bind.annotation.RequestMapping;
55
import org.springframework.web.bind.annotation.ResponseBody;
66

7+
/**
8+
*
9+
* @author JavaEdge
10+
* @date 2019年10月9日
11+
*/
712
@Controller
813
@RequestMapping("/threadLocal")
914
public class ThreadLocalController {

‎concurrency/src/main/java/com/javaedge/concurrency/example/threadPool/ThreadPoolExample1.java renamed to ‎concurrency/src/main/java/com/javaedge/concurrency/example/threadpool/ThreadPoolExample1.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
package com.javaedge.concurrency.example.threadPool;
1+
package com.javaedge.concurrency.example.threadpool;
22

33
import lombok.extern.slf4j.Slf4j;
44

55
import java.util.concurrent.ExecutorService;
66
import java.util.concurrent.Executors;
77

8+
/**
9+
*
10+
* @author JavaEdge
11+
* @date 2019年10月9日
12+
*/
813
@Slf4j
914
public class ThreadPoolExample1 {
1015

@@ -14,12 +19,7 @@ public static void main(String[] args) {
1419

1520
for (int i = 0; i < 10; i++) {
1621
final int index = i;
17-
executorService.execute(new Runnable() {
18-
@Override
19-
public void run() {
20-
log.info("task:{}", index);
21-
}
22-
});
22+
executorService.execute(() -> log.info("task:{}", index));
2323
}
2424
executorService.shutdown();
2525
}

‎concurrency/src/main/java/com/javaedge/concurrency/example/threadPool/ThreadPoolExample2.java renamed to ‎concurrency/src/main/java/com/javaedge/concurrency/example/threadpool/ThreadPoolExample2.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
package com.javaedge.concurrency.example.threadPool;
1+
package com.javaedge.concurrency.example.threadpool;
22

33
import lombok.extern.slf4j.Slf4j;
44

55
import java.util.concurrent.ExecutorService;
66
import java.util.concurrent.Executors;
77

8+
/**
9+
*
10+
* @author JavaEdge
11+
* @date 2019年10月9日
12+
*/
813
@Slf4j
914
public class ThreadPoolExample2 {
1015

@@ -14,12 +19,7 @@ public static void main(String[] args) {
1419

1520
for (int i = 0; i < 10; i++) {
1621
final int index = i;
17-
executorService.execute(new Runnable() {
18-
@Override
19-
public void run() {
20-
log.info("task:{}", index);
21-
}
22-
});
22+
executorService.execute(() -> log.info("task:{}", index));
2323
}
2424
executorService.shutdown();
2525
}

‎concurrency/src/main/java/com/javaedge/concurrency/example/threadPool/ThreadPoolExample3.java renamed to ‎concurrency/src/main/java/com/javaedge/concurrency/example/threadpool/ThreadPoolExample3.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
package com.javaedge.concurrency.example.threadPool;
1+
package com.javaedge.concurrency.example.threadpool;
22

33
import lombok.extern.slf4j.Slf4j;
44

55
import java.util.concurrent.ExecutorService;
66
import java.util.concurrent.Executors;
77

8+
/**
9+
*
10+
* @author JavaEdge
11+
* @date 2019年10月9日
12+
*/
813
@Slf4j
914
public class ThreadPoolExample3 {
1015

@@ -14,12 +19,7 @@ public static void main(String[] args) {
1419

1520
for (int i = 0; i < 10; i++) {
1621
final int index = i;
17-
executorService.execute(new Runnable() {
18-
@Override
19-
public void run() {
20-
log.info("task:{}", index);
21-
}
22-
});
22+
executorService.execute(() -> log.info("task:{}", index));
2323
}
2424
executorService.shutdown();
2525
}

‎concurrency/src/main/java/com/javaedge/concurrency/example/threadPool/ThreadPoolExample4.java renamed to ‎concurrency/src/main/java/com/javaedge/concurrency/example/threadpool/ThreadPoolExample4.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.javaedge.concurrency.example.threadPool;
1+
package com.javaedge.concurrency.example.threadpool;
22

33
import lombok.extern.slf4j.Slf4j;
44

@@ -9,6 +9,11 @@
99
import java.util.concurrent.ScheduledExecutorService;
1010
import java.util.concurrent.TimeUnit;
1111

12+
/**
13+
*
14+
* @author JavaEdge
15+
* @date 2019年10月9日
16+
*/
1217
@Slf4j
1318
public class ThreadPoolExample4 {
1419

@@ -23,12 +28,7 @@ public static void main(String[] args) {
2328
// }
2429
// }, 3, TimeUnit.SECONDS);
2530

26-
executorService.scheduleAtFixedRate(new Runnable() {
27-
@Override
28-
public void run() {
29-
log.warn("schedule run");
30-
}
31-
}, 1, 3, TimeUnit.SECONDS);
31+
executorService.scheduleAtFixedRate(() -> log.warn("schedule run"), 1, 3, TimeUnit.SECONDS);
3232
// executorService.shutdown();
3333

3434
Timer timer = new Timer();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /