Skip to content

Commit 9d23ce3

Browse files
author
cicadasmile
committed
Java并发:线程核心机制,基础概念扩展
1 parent ffe33b3 commit 9d23ce3

File tree

7 files changed

+230
-0
lines changed

7 files changed

+230
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.multy.thread.block02extend;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ExtendThread01 {
7+
public static void main(String[] args) {
8+
List<Object> dataList = new ArrayList<>() ;
9+
dataList.add("A");
10+
dataList.add("B");
11+
dataList.add("C");
12+
// 把一个大的集合按照每个子集合的2个元素切割
13+
List<List<Object>> splitList = splitList(dataList,2);
14+
for (List<Object> list:splitList){
15+
System.out.println(list);
16+
}
17+
// 多线程处理
18+
for (List<Object> childList:splitList){
19+
ListTask listTask = new ListTask(childList) ;
20+
Thread runThread = new Thread(listTask);
21+
runThread.start();
22+
}
23+
}
24+
/**
25+
* List 集合切割
26+
*/
27+
private static List<List<Object>> splitList (List<Object> list, int childSize) {
28+
if (list == null || list.size() == 0 || childSize < 1) {
29+
return null;
30+
}
31+
List<List<Object>> result = new ArrayList<>();
32+
int size = list.size();
33+
int count = (size + childSize - 1) / childSize ;
34+
for (int i = 0; i < count; i++) {
35+
List<Object> subList = list.subList(i * childSize, ((i + 1) * childSize > size ? size : childSize * (i + 1)));
36+
result.add(subList);
37+
}
38+
return result;
39+
}
40+
}
41+
class ListTask implements Runnable {
42+
private List<Object> list ;
43+
public ListTask (List<Object> list){this.list=list;}
44+
@Override
45+
public void run() {
46+
for (Object object:list){
47+
System.out.println(Thread.currentThread().getName()+"=="+object);
48+
}
49+
}
50+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.multy.thread.block02extend;
2+
3+
public class ExtendThread02 {
4+
public static void main(String[] args) {
5+
StopThread stopThread = new StopThread() ;
6+
stopThread.start();
7+
// 标记当前线程停止信号,且抛出中断异常,但没有停止
8+
stopThread.interrupt();
9+
// 判断当前线程是否已经是终止状态
10+
System.out.println("1=="+stopThread.isInterrupted());
11+
// 清除当前线程的终止信号,并判断执行该方法的线程(main)是否中断
12+
System.out.println("2=="+stopThread.interrupted());
13+
// 再次判断当前线程状态
14+
System.out.println("3=="+stopThread.isInterrupted());
15+
System.out.println("main end ...");
16+
}
17+
}
18+
class StopThread extends Thread {
19+
@Override
20+
public void run() {
21+
for (int i = 0 ; i < 10 ; i++){
22+
try {
23+
System.out.println(Thread.currentThread().getId()+"="+i);
24+
// 线程阻塞1秒
25+
Thread.sleep(1000);
26+
} catch (InterruptedException e){
27+
e.printStackTrace();
28+
}
29+
}
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.multy.thread.block02extend;
2+
3+
public class ExtendThread03 {
4+
public static void main(String[] args) {
5+
Priority01 priority01 = new Priority01();
6+
priority01.start();
7+
System.out.println("priority01="+priority01.getPriority());
8+
Priority02 priority02 = new Priority02();
9+
priority02.start();
10+
System.out.println("priority02="+priority02.getPriority());
11+
priority01.setPriority(10);
12+
priority02.setPriority(1);
13+
}
14+
}
15+
class Priority01 extends Thread {
16+
@Override
17+
public void run() {
18+
for (int i = 0 ; i < 100 ; i++){
19+
System.out.println(Thread.currentThread().getName()+";i="+i);
20+
}
21+
}
22+
}
23+
class Priority02 extends Thread {
24+
@Override
25+
public void run() {
26+
for (int a = 0 ; a < 100 ; a++){
27+
System.out.println(Thread.currentThread().getName()+";a="+a);
28+
}
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.multy.thread.block02extend;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
public class ExtendThread04 {
6+
public static void main(String[] args) {
7+
JoinThreadA joinThreadA = new JoinThreadA() ;
8+
joinThreadA.start();
9+
}
10+
}
11+
class JoinThreadA extends Thread {
12+
@Override
13+
public void run() {
14+
System.out.println("缺水中...");
15+
JoinThreadB joinThreadB = new JoinThreadB() ;
16+
joinThreadB.start();
17+
try{
18+
joinThreadB.join();
19+
} catch (Exception e){
20+
e.printStackTrace();
21+
}
22+
System.out.println("喝水中...");
23+
}
24+
}
25+
class JoinThreadB extends Thread {
26+
@Override
27+
public void run() {
28+
System.out.println("买水中...");
29+
try{
30+
TimeUnit.SECONDS.sleep(2);
31+
} catch (Exception e){
32+
e.printStackTrace();
33+
}
34+
System.out.println("买到水...");
35+
}
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.multy.thread.block02extend;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
public class ExtendThread05 {
6+
private static final ThreadLocal<Long> threadLocal = new ThreadLocal<>() ;
7+
private static void initBegin (){
8+
threadLocal.set(System.currentTimeMillis());
9+
}
10+
private static Long overTime (){
11+
return System.currentTimeMillis()-threadLocal.get();
12+
}
13+
public static void main(String[] args) throws Exception {
14+
ExtendThread05.initBegin();
15+
TimeUnit.SECONDS.sleep(3);
16+
System.out.println(ExtendThread05.overTime());
17+
}
18+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.multy.thread.block02extend;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.util.concurrent.TimeUnit;
6+
7+
public class ExtendThread06 {
8+
public static void main(String[] args) throws Exception {
9+
InputStreamReader is = new InputStreamReader(System.in);
10+
BufferedReader br = new BufferedReader(is);
11+
String value = br.readLine();
12+
CheckThread checkThread = new CheckThread(value) ;
13+
checkThread.setDaemon(true);
14+
checkThread.start();
15+
}
16+
}
17+
class CheckThread extends Thread {
18+
private String spell ;
19+
public CheckThread (String spell){
20+
this.spell = spell ;
21+
}
22+
@Override
23+
public void run() {
24+
if (spell.startsWith("cs")){
25+
System.out.println(spell+":输入正确");
26+
} else {
27+
System.out.println(spell+":输入错误");
28+
}
29+
try {
30+
TimeUnit.SECONDS.sleep(10);
31+
} catch (InterruptedException e){
32+
e.printStackTrace();
33+
}
34+
}
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.multy.thread.block02extend;
2+
3+
public class ExtendThread07 {
4+
public static void main(String[] args) {
5+
TryThread tryThread = new TryThread();
6+
tryThread.setName("try-name");
7+
// 定义运行中异常处理策略
8+
MyExe myExe = new MyExe() ;
9+
tryThread.setUncaughtExceptionHandler(myExe);
10+
tryThread.start();
11+
}
12+
}
13+
class TryThread extends Thread {
14+
@Override
15+
public void run() {
16+
try {
17+
Thread.sleep(1000);
18+
} catch (InterruptedException e){
19+
e.printStackTrace();
20+
}
21+
// 如何处理这里异常?
22+
Integer.parseInt("cicada") ;
23+
}
24+
}
25+
class MyExe implements Thread.UncaughtExceptionHandler {
26+
@Override
27+
public void uncaughtException(Thread t, Throwable e) {
28+
System.out.println(t.getName()+";异常:"+e.getMessage());
29+
}
30+
}

0 commit comments

Comments
 (0)