Skip to content

Commit a2f27c5

Browse files
committed
Add ch17 code
1 parent 0a7e7b1 commit a2f27c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1042
-0
lines changed

code/chapter17/.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/chapter17/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/chapter17/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/chapter17/.idea/uiDesigner.xml

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/chapter17/Thread.uml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Diagram>
3+
<ID>JAVA</ID>
4+
<OriginalElement>java.lang.Thread</OriginalElement>
5+
<nodes>
6+
<node x="0.0" y="0.0">java.lang.FunctionalInterface</node>
7+
<node x="88.5" y="190.0">java.lang.Thread</node>
8+
<node x="75.0" y="95.0">java.lang.Runnable</node>
9+
</nodes>
10+
<notes />
11+
<edges>
12+
<edge source="java.lang.Runnable" target="java.lang.FunctionalInterface">
13+
<point x="0.0" y="-22.5" />
14+
<point x="0.0" y="22.5" />
15+
</edge>
16+
<edge source="java.lang.Thread" target="java.lang.Runnable">
17+
<point x="0.0" y="-22.5" />
18+
<point x="0.0" y="22.5" />
19+
</edge>
20+
</edges>
21+
<settings layout="Hierarchic Group" zoom="1.0" x="160.0" y="117.5" />
22+
<SelectedNodes>
23+
<node>java.lang.Thread</node>
24+
<node>java.lang.Runnable</node>
25+
</SelectedNodes>
26+
<Categories />
27+
<SCOPE>All</SCOPE>
28+
<VISIBILITY>private</VISIBILITY>
29+
</Diagram>
30+

code/chapter17/chapter17.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.hspedu;
2+
3+
4+
public class CpuNum {
5+
public static void main(String[] args) {
6+
7+
Runtime runtime = Runtime.getRuntime();
8+
//获取当前电脑的cpu数量/核心数
9+
int cpuNums = runtime.availableProcessors();
10+
System.out.println("当前有cpu 个数=" + cpuNums);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.hspedu.exit_;
2+
3+
public class ThreadExit_ {
4+
public static void main(String[] args) throws InterruptedException {
5+
T t1 = new T();
6+
t1.start();
7+
8+
// 如果希望 main 线程去控制 t1 线程的终止, 必须可以修改 loop
9+
// 让 t1 退出 run 方法,从而终止 t1 线程 -> 通知方式
10+
// 让主线程休眠 10 秒,再通知 t1 线程退出
11+
System.out.println("main线程休眠10s...");
12+
Thread.sleep(10 * 1000);
13+
t1.setLoop(false);
14+
}
15+
}
16+
17+
class T extends Thread {
18+
private int count = 0;
19+
// 设置一个控制变量
20+
private boolean loop = true;
21+
@Override
22+
public void run() {
23+
while (loop) {
24+
25+
try {
26+
Thread.sleep(50);// 让当前线程休眠50ms
27+
} catch (InterruptedException e) {
28+
e.printStackTrace();
29+
}
30+
System.out.println("T 运行中...." + (++count));
31+
}
32+
}
33+
public void setLoop(boolean loop) {
34+
this.loop = loop;
35+
}
36+
}
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.hspedu.homework;
2+
3+
import java.util.Scanner;
4+
5+
6+
public class Homework01 {
7+
public static void main(String[] args) {
8+
A a = new A();
9+
B b = new B(a);//一定要注意.
10+
a.start();
11+
b.start();
12+
}
13+
}
14+
15+
//创建A线程类
16+
class A extends Thread {
17+
private boolean loop = true;
18+
19+
@Override
20+
public void run() {
21+
//输出1-100数字
22+
while (loop) {
23+
System.out.println((int)(Math.random() * 100 + 1));
24+
//休眠
25+
try {
26+
Thread.sleep(1000);
27+
} catch (InterruptedException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
System.out.println("a线程退出...");
32+
33+
}
34+
35+
public void setLoop(boolean loop) {//可以修改loop变量
36+
this.loop = loop;
37+
}
38+
}
39+
40+
//直到第2个线程从键盘读取了“Q”命令
41+
class B extends Thread {
42+
private A a;
43+
private Scanner scanner = new Scanner(System.in);
44+
45+
public B(A a) {//构造器中,直接传入A类对象
46+
this.a = a;
47+
}
48+
49+
@Override
50+
public void run() {
51+
while (true) {
52+
//接收到用户的输入
53+
System.out.println("请输入你指令(Q)表示退出:");
54+
char key = scanner.next().toUpperCase().charAt(0);
55+
if(key == 'Q') {
56+
//以通知的方式结束a线程
57+
a.setLoop(false);
58+
System.out.println("b线程退出.");
59+
break;
60+
}
61+
}
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.hspedu.homework;
2+
3+
public class Homework02 {
4+
public static void main(String[] args) {
5+
T t = new T();
6+
Thread thread1 = new Thread(t);
7+
thread1.setName("t1");
8+
Thread thread2 = new Thread(t);
9+
thread2.setName("t2");
10+
thread1.start();
11+
thread2.start();
12+
}
13+
}
14+
15+
//1.因为这里涉及到多个线程共享资源,所以我们使用实现Runnable方式
16+
//2. 每次取出 1000
17+
class T implements Runnable {
18+
private int money = 10000;
19+
20+
@Override
21+
public void run() {
22+
while (true) {
23+
24+
//解读
25+
//1. 这里使用 synchronized 实现了线程同步
26+
//2. 当多个线程执行到这里时,就会去争夺 this对象锁
27+
//3. 哪个线程争夺到(获取)this对象锁,就执行 synchronized 代码块, 执行完后,会释放this对象锁
28+
//4. 争夺不到this对象锁,就blocked ,准备继续争夺
29+
//5. this对象锁是非公平锁.
30+
31+
synchronized (this) {//
32+
//判断余额是否够
33+
if (money < 1000) {
34+
System.out.println("余额不足");
35+
break;
36+
}
37+
38+
money -= 1000;
39+
System.out.println(Thread.currentThread().getName() + " 取出了1000 当前余额=" + money);
40+
}
41+
42+
//休眠1s
43+
try {
44+
Thread.sleep(1000);
45+
} catch (InterruptedException e) {
46+
e.printStackTrace();
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)