forked from mio4/learn-java
-
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
Showing
38 changed files
with
206 additions
and
642 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# 过滤所有.class文件 | ||
# 过滤所有jar包 | ||
*.class | ||
*.jar | ||
*.jar | ||
*.idea |
File renamed without changes.
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,7 @@ | ||
package hire.testAtomic; | ||
|
||
public class test{ | ||
public static void main(String[] args){ | ||
System.out.println("?"); | ||
} | ||
} |
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,30 @@ | ||
package hire.testAtomic; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
//TODO 为什么不是Runnable接口的类都可以作为线程池参数,运行 | ||
class Count{ | ||
// private Integer count = 0; | ||
private AtomicInteger count = new AtomicInteger(0); | ||
public Integer getCount(){ | ||
// return count; | ||
return count.get(); | ||
} | ||
public void increase(){ | ||
// count++; | ||
count.incrementAndGet(); | ||
} | ||
} | ||
|
||
public class withoutAtomic { | ||
public static void main(String[] args){ | ||
ExecutorService service = Executors.newCachedThreadPool(); | ||
Count count = new Count(); | ||
for(int i=0; i < 100; i++){ | ||
service.execute(() -> count.increase()); | ||
} | ||
System.out.println(count.getCount()); | ||
} | ||
} |
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 @@ | ||
package hire.testRunnable; | ||
|
||
class Seller2 implements Runnable{ | ||
private int ticket = 5; | ||
public void run(){ | ||
while(ticket > 0){ | ||
sellTicket(); | ||
} | ||
} | ||
|
||
private synchronized void sellTicket(){ | ||
System.out.println("current thread:" + Thread.currentThread() + " thread ticket = " + ticket); | ||
ticket--; | ||
} | ||
} | ||
|
||
public class useRunnable { | ||
public static void main(String[] args){ | ||
Seller2 s = new Seller2(); | ||
new Thread(s).start(); | ||
new Thread(s).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package hire.testRunnable; | ||
|
||
class Seller extends Thread{ | ||
private int ticket = 5; | ||
public void run(){ | ||
while(true){ | ||
System.out.println("thread ticket = " + ticket); | ||
ticket--; | ||
if(ticket < 0){ | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public class useThread { | ||
public static void main(String[] args){ | ||
new Seller().start(); | ||
new Seller().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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package hire.testThreadLocal; | ||
|
||
class Bank { | ||
ThreadLocal<Integer> t = new ThreadLocal<Integer>(){ | ||
@Override | ||
protected Integer initialValue(){ | ||
return 100; | ||
} | ||
}; | ||
|
||
public int get(){ | ||
return t.get(); | ||
} | ||
|
||
public void set(){ | ||
t.set(t.get() + 10); | ||
} | ||
} | ||
|
||
class Transfer implements Runnable{ | ||
Bank bank; | ||
|
||
public Transfer(Bank bank){ | ||
this.bank = bank; | ||
} | ||
|
||
@Override | ||
public void run(){ | ||
for(int i=0; i < 10; i++){ | ||
bank.set(); | ||
System.out.println(Thread.currentThread() + " " + bank.get()); | ||
} | ||
} | ||
} | ||
|
||
public class testThreadLocal{ | ||
public static void main(String[] args) throws InterruptedException { | ||
Bank bank = new Bank(); | ||
Transfer t = new Transfer(bank); | ||
Thread t1 = new Thread(t); | ||
t1.start(); | ||
Thread t2 = new Thread(t); | ||
t2.start(); | ||
t1.join(); | ||
t2.join(); | ||
System.out.println(bank.get()); | ||
} | ||
} | ||
|
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,20 @@ | ||
package hire.testVolatile; | ||
|
||
public class NoVisibility { | ||
private static boolean ready; | ||
private static int number; | ||
private static class ReaderThread extends Thread { | ||
@Override | ||
public void run() { | ||
while(!ready) { | ||
Thread.yield(); | ||
} | ||
System.out.println(number); | ||
} | ||
} | ||
public static void main(String[] args) { | ||
new ReaderThread().start(); | ||
number = 42; | ||
ready = true; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...va多线程设计模式/MultiThread/src/Chap0/Bank.java → Concurrency/src/notes/Chap0/Bank.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap0; | ||
package notes.Chap0; | ||
|
||
public class Bank { | ||
private int money; | ||
|
2 changes: 1 addition & 1 deletion
2
...模式/MultiThread/src/Chap0/PrintThread.java → Concurrency/src/notes/Chap0/PrintThread.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap0; | ||
package notes.Chap0; | ||
|
||
public class PrintThread extends Thread{ | ||
private String message; | ||
|
2 changes: 1 addition & 1 deletion
2
...线程设计模式/MultiThread/src/Chap0/Printer.java → Concurrency/src/notes/Chap0/Printer.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap0; | ||
package notes.Chap0; | ||
|
||
public class Printer implements Runnable{ | ||
private String message; | ||
|
2 changes: 1 addition & 1 deletion
2
...a多线程设计模式/MultiThread/src/Chap0/Start.java → Concurrency/src/notes/Chap0/Start.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap0; | ||
package notes.Chap0; | ||
|
||
class MyThread extends Thread{ | ||
@Override | ||
|
2 changes: 1 addition & 1 deletion
2
...va多线程设计模式/MultiThread/src/Chap1/Gate.java → Concurrency/src/notes/Chap1/Gate.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap1; | ||
package notes.Chap1; | ||
|
||
public class Gate { | ||
private int counter = 0; | ||
|
2 changes: 1 addition & 1 deletion
2
...程设计模式/MultiThread/src/Chap1/SempMain.java → Concurrency/src/notes/Chap1/SempMain.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap1; | ||
package notes.Chap1; | ||
|
||
import java.util.Random; | ||
import java.util.concurrent.Semaphore; | ||
|
2 changes: 1 addition & 1 deletion
2
...a多线程设计模式/MultiThread/src/Chap2/Amain.java → Concurrency/src/notes/Chap2/Amain.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap2; | ||
package notes.Chap2; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
|
2 changes: 1 addition & 1 deletion
2
...a多线程设计模式/MultiThread/src/Chap2/Cmain.java → Concurrency/src/notes/Chap2/Cmain.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap2; | ||
package notes.Chap2; | ||
|
||
|
||
|
||
|
2 changes: 1 addition & 1 deletion
2
...a多线程设计模式/MultiThread/src/Chap2/Dmain.java → Concurrency/src/notes/Chap2/Dmain.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap2; | ||
package notes.Chap2; | ||
|
||
public class Dmain { | ||
private final static Long CALL_COUNT = 1000000000L; | ||
|
2 changes: 1 addition & 1 deletion
2
...a多线程设计模式/MultiThread/src/Chap2/Emain.java → Concurrency/src/notes/Chap2/Emain.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap2; | ||
package notes.Chap2; | ||
|
||
/** | ||
* StringBuffer不是immutable类 | ||
|
2 changes: 1 addition & 1 deletion
2
...va多线程设计模式/MultiThread/src/Chap2/test.java → Concurrency/src/notes/Chap2/test.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap2; | ||
package notes.Chap2; | ||
|
||
class t{ | ||
private final String s; | ||
|
2 changes: 1 addition & 1 deletion
2
...a多线程设计模式/MultiThread/src/Chap3/Amain.java → Concurrency/src/notes/Chap3/Amain.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap3; | ||
package notes.Chap3; | ||
|
||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
2 changes: 1 addition & 1 deletion
2
...a多线程设计模式/MultiThread/src/Chap4/Amain.java → Concurrency/src/notes/Chap4/Amain.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap4; | ||
package notes.Chap4; | ||
|
||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
2 changes: 1 addition & 1 deletion
2
...a多线程设计模式/MultiThread/src/Chap5/Amain.java → Concurrency/src/notes/Chap5/Amain.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap5; | ||
package notes.Chap5; | ||
|
||
import java.util.Random; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...a多线程设计模式/MultiThread/src/Chap5/Bmain.java → Concurrency/src/notes/Chap5/Bmain.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap5; | ||
package notes.Chap5; | ||
|
||
public class Bmain { | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...a多线程设计模式/MultiThread/src/Chap5/Cmain.java → Concurrency/src/notes/Chap5/Cmain.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap5; | ||
package notes.Chap5; | ||
|
||
/** | ||
* 测试Sleep方法被Interrupted方法打断操作 | ||
|
2 changes: 1 addition & 1 deletion
2
...va多线程设计模式/MultiThread/src/Chap6/Main.java → Concurrency/src/notes/Chap6/Main.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap6; | ||
package notes.Chap6; | ||
|
||
import java.util.Random; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...a多线程设计模式/MultiThread/src/Chap7/Amain.java → Concurrency/src/notes/Chap7/Amain.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Chap7; | ||
package notes.Chap7; | ||
|
||
class Host{ | ||
private final Helper helper = new Helper(); | ||
|
2 changes: 1 addition & 1 deletion
2
...a多线程设计模式/MultiThread/src/PlusB/Bmain.java → Concurrency/src/notes/PlusB/Bmain.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package PlusB; | ||
package notes.PlusB; | ||
|
||
class Runner extends Thread{ | ||
private volatile boolean quit = false; | ||
|
2 changes: 1 addition & 1 deletion
2
...va多线程设计模式/MultiThread/src/PlusB/Main.java → Concurrency/src/notes/PlusB/Main.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package PlusB; | ||
package notes.PlusB; | ||
|
||
class Something{ | ||
private int x = 0; | ||
|
2 changes: 1 addition & 1 deletion
2
...式/MultiThread/src/PlusB/NoVisibility.java → ...urrency/src/notes/PlusB/NoVisibility.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package PlusB; | ||
package notes.PlusB; | ||
|
||
|
||
public class NoVisibility { | ||
|
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,5 @@ | ||
public class test { | ||
public static void main(String[] args){ | ||
System.out.println("??"); | ||
} | ||
} |
Oops, something went wrong.