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
910790b
commit 5333702
Showing
6 changed files
with
227 additions
and
34 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
code/Java/java-concurrency/src/main/java/com/heibaiying/createThread/J1_Method01.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,16 @@ | ||
package com.heibaiying.createThread; | ||
|
||
public class J1_Method01 { | ||
public static void main(String[] args) { | ||
System.out.println("Main线程的ID为:" + Thread.currentThread().getId()); | ||
Thread thread = new Thread(new CustomRunner()); | ||
thread.start(); | ||
} | ||
} | ||
|
||
class CustomRunner implements Runnable { | ||
@Override | ||
public void run() { | ||
System.out.println("CustomThread线程的ID为:" + Thread.currentThread().getId()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
code/Java/java-concurrency/src/main/java/com/heibaiying/createThread/J2_Method02.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,16 @@ | ||
package com.heibaiying.createThread; | ||
|
||
public class J2_Method02 { | ||
public static void main(String[] args) { | ||
System.out.println("Main线程的ID为:" + Thread.currentThread().getId()); | ||
CustomThread customThread = new CustomThread(); | ||
customThread.start(); | ||
} | ||
} | ||
|
||
class CustomThread extends Thread { | ||
@Override | ||
public void run() { | ||
System.out.println("CustomThread线程的ID为:" + Thread.currentThread().getId()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
.../java-concurrency/src/main/java/com/heibaiying/threeCharacteristics/J1_RaceCondition.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,34 @@ | ||
package com.heibaiying.threeCharacteristics; | ||
|
||
/** | ||
* 竞态 | ||
*/ | ||
public class J1_RaceCondition { | ||
|
||
private static int i = 0; | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
IncreaseTask task = new IncreaseTask(); | ||
Thread thread1 = new Thread(task); | ||
Thread thread2 = new Thread(task); | ||
thread1.start(); | ||
thread2.start(); | ||
//等待线程结束后 才打印返回值 | ||
thread1.join(); | ||
thread2.join(); | ||
System.out.println(i); | ||
} | ||
|
||
static class IncreaseTask implements Runnable { | ||
@Override | ||
public void run() { | ||
for (int j = 0; j < 100000; j++) { | ||
inc(); | ||
} | ||
} | ||
|
||
private void inc() { | ||
i++; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
code/Java/java-concurrency/src/main/java/com/heibaiying/threeCharacteristics/J2_Atomic.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,36 @@ | ||
package com.heibaiying.threeCharacteristics; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* 原子性 | ||
*/ | ||
public class J2_Atomic { | ||
|
||
private static AtomicInteger i = new AtomicInteger(0); | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
IncreaseTask task = new IncreaseTask(); | ||
Thread thread1 = new Thread(task); | ||
Thread thread2 = new Thread(task); | ||
thread1.start(); | ||
thread2.start(); | ||
//等待线程结束后 才打印返回值 | ||
thread1.join(); | ||
thread2.join(); | ||
System.out.println(i); | ||
} | ||
|
||
static class IncreaseTask implements Runnable { | ||
@Override | ||
public void run() { | ||
for (int j = 0; j < 100000; j++) { | ||
inc(); | ||
} | ||
} | ||
|
||
private void inc() { | ||
i.incrementAndGet(); | ||
} | ||
} | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.