forked from heibaiying/Full-Stack-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e266780
commit 2ffc0a3
Showing
29 changed files
with
342 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?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>com.heibaiying</groupId> | ||
<artifactId>java-concurrency</artifactId> | ||
<version>1.0</version> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>8</source> | ||
<target>8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
17 changes: 17 additions & 0 deletions
17
code/Java/java-concurrency/src/main/java/com/heibaiying/interrupt/J1_Interrupt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.heibaiying.interrupt; | ||
|
||
/** | ||
* interrupt() 只是设置中断标志位,并不能中断线程 | ||
*/ | ||
public class J1_Interrupt { | ||
public static void main(String[] args) throws InterruptedException { | ||
Thread thread = new Thread(() -> { | ||
while (true) { | ||
System.out.println("子线程打印"); | ||
} | ||
}); | ||
thread.start(); | ||
Thread.sleep(10); | ||
thread.interrupt(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
code/Java/java-concurrency/src/main/java/com/heibaiying/interrupt/J2_IsInterrupted.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.heibaiying.interrupt; | ||
|
||
/** | ||
* isInterrupted() 用于检查当前线程是否存在中断标志位 | ||
*/ | ||
public class J2_IsInterrupted { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
Thread thread = new Thread(() -> { | ||
while (!Thread.currentThread().isInterrupted()) { | ||
System.out.println("子线程打印"); | ||
} | ||
}); | ||
thread.start(); | ||
Thread.sleep(10); | ||
thread.interrupt(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
code/Java/java-concurrency/src/main/java/com/heibaiying/interrupt/J3_Interrupted.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.heibaiying.interrupt; | ||
|
||
/** | ||
* 判断当前线程的中断状态,并清除当前线程的中断标志位 | ||
*/ | ||
public class J3_Interrupted { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
Thread thread = new Thread(() -> { | ||
while (!Thread.interrupted() || !Thread.currentThread().isInterrupted()) { | ||
System.out.println("子线程打印"); | ||
} | ||
}); | ||
thread.start(); | ||
Thread.sleep(10); | ||
thread.interrupt(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
code/Java/java-concurrency/src/main/java/com/heibaiying/waitAndNotify/J1_Normal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.heibaiying.waitAndNotify; | ||
|
||
/** | ||
* 正常情况下 | ||
*/ | ||
public class J1_Normal { | ||
|
||
private static final Object object = new Object(); | ||
|
||
public static void main(String[] args) { | ||
new Thread(() -> { | ||
synchronized (object) { | ||
System.out.println("线程1"); | ||
} | ||
}).start(); | ||
new Thread(() -> { | ||
synchronized (object) { | ||
System.out.println("线程2"); | ||
} | ||
}).start(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...Java/java-concurrency/src/main/java/com/heibaiying/waitAndNotify/J2_KeepObjectLocked.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.heibaiying.waitAndNotify; | ||
|
||
/** | ||
* 锁住对象不释放 | ||
*/ | ||
public class J2_KeepObjectLocked { | ||
|
||
private static final Object object = new Object(); | ||
|
||
public static void main(String[] args) { | ||
new Thread(() -> { | ||
synchronized (object) { | ||
while (true) { | ||
try { | ||
Thread.sleep(1000); | ||
System.out.println("线程1"); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
}).start(); | ||
new Thread(() -> { | ||
synchronized (object) { | ||
System.out.println("线程2"); | ||
} | ||
}).start(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
code/Java/java-concurrency/src/main/java/com/heibaiying/waitAndNotify/J3_WaitAndNotify.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.heibaiying.waitAndNotify; | ||
|
||
/** | ||
* 等待与唤醒 | ||
*/ | ||
public class J3_WaitAndNotify { | ||
|
||
private static final Object object = new Object(); | ||
|
||
public static void main(String[] args) { | ||
new Thread(() -> { | ||
synchronized (object) { | ||
try { | ||
System.out.println("对象object等待"); | ||
object.wait(); | ||
System.out.println("线程1后续操作"); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}).start(); | ||
new Thread(() -> { | ||
synchronized (object) { | ||
System.out.println("对象object唤醒"); | ||
object.notify(); | ||
System.out.println("线程2后续操作"); | ||
} | ||
}).start(); | ||
} | ||
} | ||
|
52 changes: 52 additions & 0 deletions
52
...ava/java-concurrency/src/main/java/com/heibaiying/waitAndNotify/J4_AlternatePrinting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.heibaiying.waitAndNotify; | ||
|
||
/** | ||
* 利用 wait 和 Notify 实现交替打印 | ||
*/ | ||
public class J4_AlternatePrinting { | ||
|
||
private static final Object object = new Object(); | ||
private volatile static boolean flag = false; | ||
private static int i = 0; | ||
private static int threshold = 1000000; | ||
|
||
public static void main(String[] args) { | ||
new Thread(() -> { | ||
synchronized (object) { | ||
try { | ||
while (i <= threshold) { | ||
if (flag) { | ||
object.wait(); | ||
} else { | ||
object.notify(); | ||
System.out.println("Thread 1 : " + i++); | ||
flag = !flag; | ||
} | ||
} | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}).start(); | ||
new Thread(() -> { | ||
synchronized (object) { | ||
while (i <= threshold) { | ||
try { | ||
while (i <= threshold) { | ||
if (flag) { | ||
object.notify(); | ||
System.out.println("Thread 2 : " + i++); | ||
flag = !flag; | ||
} else { | ||
object.wait(); | ||
} | ||
} | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
}).start(); | ||
} | ||
} | ||
|
43 changes: 43 additions & 0 deletions
43
code/Java/java-concurrency/src/main/java/com/heibaiying/waitAndNotify/J5_NotifyAll.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.heibaiying.waitAndNotify; | ||
|
||
/** | ||
* 全部唤醒 | ||
*/ | ||
public class J5_NotifyAll { | ||
|
||
private static final Object object = new Object(); | ||
|
||
public static void main(String[] args) { | ||
new Thread(() -> { | ||
synchronized (object) { | ||
try { | ||
System.out.println("对象object在线程1等待"); | ||
object.wait(); | ||
System.out.println("线程1后续操作"); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}).start(); | ||
new Thread(() -> { | ||
synchronized (object) { | ||
try { | ||
System.out.println("对象object在线程2等待"); | ||
object.wait(); | ||
System.out.println("线程2后续操作"); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}).start(); | ||
new Thread(() -> { | ||
synchronized (object) { | ||
System.out.println("对象object唤醒"); | ||
// 如果是object.notify()则是随机唤醒任意一个等待 | ||
object.notifyAll(); | ||
System.out.println("线程3后续操作"); | ||
} | ||
}).start(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.