Skip to content

Commit d55098c

Browse files
committed
no message
1 parent 545572f commit d55098c

8 files changed

Lines changed: 247 additions & 9 deletions

File tree

src/main/java/org/tj/designpatterns/creater/singleton/PerfectSingleton.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.tj.designpatterns.creater.singleton;
22

3+
import java.text.NumberFormat;
4+
35
/**
46
* Created by 001 on 16/8/8.
57
*/
@@ -9,7 +11,7 @@ public class PerfectSingleton {
911
// 这个类的加载过程是线程互斥的。这样当我们第一次调用getInstance的时候,JVM能够帮我们保证instance只被创建一次,
1012
// 并且会保证把赋值给instance的内存初始化完毕,这样我们就不用担心上面的问题。同时该方法也只会在第一次调用的时候使用互斥机制,
1113
// 这样就解决了低性能问题。这样我们暂时总结一个相对完美的单例模式:
12-
14+
// 该种实现方式与饿汉式区别是 静态内部类是一个很特殊的类 它只有在被第一次被引用的时候才会加载类文件信息 这样就实现了线程安全的延迟加载。
1315
private PerfectSingleton(){
1416

1517
}
@@ -28,5 +30,11 @@ public Object readResolve() {
2830
return getInstance();
2931
}
3032

33+
public static void main(String[] args) {
34+
//饿汉式
35+
Runtime.getRuntime();
36+
NumberFormat.getInstance();
37+
38+
}
3139

3240
}
Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,75 @@
11
package org.tj.study.thread.locks;
22

3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
5+
import java.util.concurrent.locks.Condition;
6+
import java.util.concurrent.locks.Lock;
7+
38
/**
49
* Created by 001 on 16/8/24.
510
*/
6-
public class TwinsLock {
11+
public class TwinsLock implements Lock {
12+
13+
private final Sync sync = new Sync(3);
14+
private class Sync extends AbstractQueuedSynchronizer{
15+
16+
Sync(int count){
17+
if (count<=0){
18+
throw new IllegalArgumentException("count must be >0");
19+
}
20+
setState(count);
21+
}
22+
23+
24+
public int tryAcquireShared(int reduceCount){
25+
for (;;){
26+
int current = getState();
27+
int newCount = current - reduceCount;
28+
if (newCount < 0 || compareAndSetState(current,newCount)){
29+
return newCount;
30+
}
31+
}
32+
}
33+
34+
public boolean tryReleaseShared(int returnCount){
35+
for (;;){
36+
int current = getState();
37+
int newCount = current + returnCount;
38+
if (compareAndSetState(current,newCount)){
39+
return true;
40+
}
41+
}
42+
}
43+
}
44+
45+
@Override
46+
public void lock() {
47+
sync.acquireShared(1);
48+
}
49+
50+
51+
@Override
52+
public void lockInterruptibly() throws InterruptedException {
53+
54+
}
55+
56+
@Override
57+
public boolean tryLock() {
58+
return false;
59+
}
60+
61+
@Override
62+
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
63+
return false;
64+
}
65+
66+
@Override
67+
public void unlock() {
68+
sync.releaseShared(1);
69+
}
70+
71+
@Override
72+
public Condition newCondition() {
73+
return null;
74+
}
775
}

src/main/java/org/tj/study/thread/produceconsumer/Test.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,34 @@
44
* Created by 001 on 16/8/23.
55
*/
66
public class Test {
7+
8+
public static void main(String[] args) {
9+
Product product = new Product();
10+
11+
// Producer producer1 = new Producer(product,10);
12+
// Producer producer2 = new Producer(product,20);
13+
// Producer producer3 = new Producer(product,30);
14+
15+
// Consumer consumer1 = new Consumer(product,30);
16+
// Consumer consumer2 = new Consumer(product,20);
17+
// Consumer consumer3 = new Consumer(product,10);
18+
19+
Thread producer1 = new Thread(new Producer(product,10),"producer1");
20+
Thread producer2 = new Thread(new Producer(product,20),"producer2");
21+
Thread producer3 = new Thread(new Producer(product,30),"producer3");
22+
23+
Thread consumer1 = new Thread(new Consumer(product,30),"consumer1");
24+
Thread consumer2 = new Thread(new Consumer(product,20),"consumer2");
25+
Thread consumer3 = new Thread(new Consumer(product,10),"consumer3");
26+
27+
28+
producer1.start();
29+
producer2.start();
30+
producer3.start();
31+
32+
consumer1.start();
33+
consumer2.start();
34+
consumer3.start();
35+
36+
}
737
}
Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,71 @@
11
package org.tj.study.thread.readfile;
22

3+
import java.io.*;
4+
35
/**
46
* Created by 001 on 16/8/22.
57
*/
6-
public class ReadFile {
8+
public class ReadFile implements Runnable{
9+
10+
int sum = 0;
11+
BufferedReader bufferedReader;
12+
String str;
713

8-
public static void main(String[] args) {
9-
FileR
14+
public ReadFile(String filename){
15+
try {
16+
bufferedReader = new BufferedReader(new FileReader(new File(filename)));
17+
} catch (FileNotFoundException e) {
18+
e.printStackTrace();
19+
}
1020
}
1121

22+
static void readfile(String fileName) throws IOException {
23+
BufferedReader bufferedReader1 = new BufferedReader(new FileReader(new File(fileName)));
24+
int sum = 0;
25+
String str;
26+
while ((str = bufferedReader1.readLine())!=null){
27+
sum += Integer.valueOf(str);
28+
}
29+
30+
}
31+
32+
public static void main(String[] args) throws IOException {
33+
34+
for (int i=0;i<8;i++){
35+
long start = System.currentTimeMillis();
36+
readfile("thread1.txt");
37+
readfile("thread2.txt");
38+
System.out.println("单线程耗时: "+(System.currentTimeMillis() - start));
39+
40+
start = System.currentTimeMillis();
41+
ReadFile readFile1 = new ReadFile("thread1.txt");
42+
ReadFile readFile2 = new ReadFile("thread2.txt");
43+
Thread thread1 = new Thread(readFile1);
44+
Thread thread2 = new Thread(readFile2);
45+
thread1.start();
46+
thread2.start();
47+
try {
48+
thread1.join();
49+
thread2.join();
50+
} catch (InterruptedException e) {
51+
e.printStackTrace();
52+
}
53+
System.out.println("多线程耗时: "+(System.currentTimeMillis() - start));
54+
// System.gc();
55+
}
56+
57+
58+
}
59+
60+
61+
@Override
62+
public void run() {
63+
try {
64+
while ((str = bufferedReader.readLine())!=null){
65+
sum += Integer.valueOf(str);
66+
}
67+
} catch (IOException e) {
68+
e.printStackTrace();
69+
}
70+
}
1271
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
11
package org.tj.study.thread.semaphore;
22

3+
import java.util.concurrent.Executor;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.Semaphore;
6+
37
/**
48
* Created by 001 on 16/8/24.
59
*/
610
public class DemoSemaphore {
11+
12+
private static final int THREAD_COUNT = 30;
13+
private static Executor threadPool =Executors
14+
.newFixedThreadPool(THREAD_COUNT);
15+
private static Semaphore s = new Semaphore(10);
16+
17+
18+
public static void main(String[] args) {
19+
for (int i = 0; i< THREAD_COUNT; i++) {
20+
threadPool.execute(new Runnable() {
21+
@Override
22+
public void run() {
23+
try {
24+
s.acquire();
25+
System.out.println("save data");
26+
s.release();
27+
} catch (InterruptedException e) {
28+
}
29+
}
30+
});
31+
}
32+
// threadPool.();
33+
}
34+
735
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,50 @@
11
package org.tj.study.thread.threadlocal;
22

3+
4+
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
5+
import java.util.concurrent.locks.Lock;
6+
import java.util.concurrent.locks.ReentrantLock;
7+
38
/**
49
* Created by 001 on 16/8/23.
510
*/
611
public class DemoThreadLocal {
12+
Long signal = Long.valueOf(1);
13+
ThreadLocal<Long> threadLocal = new ThreadLocal<>();
14+
15+
public Long getSignal() {
16+
return signal;
17+
}
18+
19+
public void setSignal(long signal) {
20+
this.signal = signal;
21+
}
22+
23+
public Long getThreadLocal() {
24+
return threadLocal.get();
25+
}
26+
27+
public void setThreadLocal(long local) {
28+
this.threadLocal.set(local);
29+
}
30+
31+
public static void main(String[] args) throws InterruptedException {
32+
DemoThreadLocal demoThreadLocal = new DemoThreadLocal();
33+
demoThreadLocal.setSignal(1);
34+
demoThreadLocal.setThreadLocal(1);
35+
// ReentrantLock
36+
System.out.println(Thread.currentThread().getName()+" "+demoThreadLocal.getSignal()+" "+demoThreadLocal.getThreadLocal());
37+
38+
Thread thread = new Thread(()->{
39+
demoThreadLocal.setSignal(2);
40+
demoThreadLocal.setThreadLocal(2);
41+
System.out.println(Thread.currentThread().getName()+" "+demoThreadLocal.getSignal()+" "+demoThreadLocal.getThreadLocal());
42+
});
43+
44+
thread.start();
45+
thread.join();
46+
System.out.println(Thread.currentThread().getName()+" "+demoThreadLocal.getSignal()+" "+demoThreadLocal.getThreadLocal());
47+
48+
49+
}
750
}

src/main/java/org/tj/study/thread/ticket/one/ThreadA.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public ThreadA(String name){
1717

1818
@Override
1919
public void run() {
20-
while (i<5){
20+
while (i<100){
2121
// synchronized (this){
2222
System.out.println(Thread.currentThread().getName()+":" + i++);
2323
// }
@@ -29,8 +29,8 @@ public static void main(String[] args) {
2929
ThreadA b = new ThreadA("b");
3030
ThreadA c = new ThreadA("c");
3131
Thread threadA = new Thread(a,"a");
32-
Thread threadB = new Thread(a,"b");
33-
Thread threadC = new Thread(a,"c");
32+
Thread threadB = new Thread(b,"b");
33+
Thread threadC = new Thread(c,"c");
3434

3535
threadA.start();
3636
threadB.start();

src/main/java/org/tj/study/thread/ticket/one/TicketSell.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ public void sellTicket() throws InterruptedException {
1313
// System.out.println(remian);
1414
// System.out.println(remian%8);
1515
if (remian>0 && remian%8!=0){
16+
// if (remian>0){
1617
System.out.println(Thread.currentThread().getName() + " 正在出售第 " + remian-- + " 票");
1718
this.wait();
19+
System.out.println(Thread.currentThread().getName() + " 被唤醒");
1820
}else {
1921
System.out.println(Thread.currentThread().getName() + " 正在出售第 " + remian-- + " 票");
2022
remian--;
@@ -39,7 +41,7 @@ public static void main(String[] args) throws InterruptedException {
3941
Thread[] threads = new Thread[9];
4042
for (int i=0;i<9;i++){
4143
threads[i] = new Thread(ticketSell,String.valueOf(i));
42-
threads[i].setPriority(i+1);
44+
// threads[i].setPriority(i+1);
4345
}
4446
for (int i=0;i<threads.length;i++){
4547
threads[i].start();

0 commit comments

Comments
 (0)