Skip to content

Commit 545572f

Browse files
committed
no message
1 parent a03dcc2 commit 545572f

5 files changed

Lines changed: 166 additions & 3 deletions

File tree

src/main/java/org/tj/study/enumtest/Weekday.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,35 @@
33
/**
44
* Created by 001 on 16/8/22.
55
*/
6-
public class Weekday {
6+
public enum Weekday {
7+
Monday(1),Tuesday(2),Wednesday(3),Thuresday(4),Friday(5),Saturday(6),Sunday(7);
8+
int value;
9+
10+
private Weekday(int value){
11+
this.value = value;
12+
}
13+
14+
public int getValue() {
15+
return value;
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return "Weekday{" +
21+
"value=" + value +
22+
'}';
23+
}
724
}
25+
26+
class Test{
27+
public static void main(String[] args) {
28+
Weekday weekday = Weekday.valueOf("Wednesday");
29+
System.out.println(weekday.getValue());
30+
31+
Weekday[] weekdays = Weekday.values();
32+
for (Weekday weekday1:weekdays){
33+
34+
}
35+
36+
}
37+
}

src/main/java/org/tj/study/thread/abcd/join/DemoJoin.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package org.tj.study.thread.abcd.join;
22

3+
import java.util.concurrent.TimeUnit;
4+
35
/**
46
* Created by 001 on 16/8/23.
57
*/
68
public class DemoJoin implements Runnable{
79

10+
// threadX.join 代表当前线程要等待X线程结束才能从join中返回继续执行
11+
812
Thread thread;
913

1014
public DemoJoin(Thread thread){
1115
this.thread = thread;
1216
}
1317

14-
public static void main(String[] args) {
18+
public static void main(String[] args) throws InterruptedException {
1519
Thread thread1 = Thread.currentThread();
1620
Thread threadA = new Thread(new DemoJoin(thread1),"A");
1721
Thread threadB = new Thread(new DemoJoin(threadA),"B");
@@ -22,6 +26,8 @@ public static void main(String[] args) {
2226
threadB.start();
2327
threadC.start();
2428
threadD.start();
29+
TimeUnit.SECONDS.sleep(5);
30+
System.out.println(Thread.currentThread().getName() + " terminate.");
2531
}
2632

2733
@Override
Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,51 @@
11
package org.tj.study.thread.locks;
22

3+
import sun.awt.Mutex;
4+
5+
import java.util.HashMap;
6+
import java.util.concurrent.ConcurrentHashMap;
7+
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
8+
import java.util.concurrent.locks.Lock;
9+
import java.util.concurrent.locks.ReentrantLock;
10+
311
/**
412
* Created by 001 on 16/8/24.
513
*/
6-
public class DemoLock {
14+
public class DemoLock implements Runnable{
15+
16+
// List<String> list = new ArrayList<>();
17+
int i = 0;
18+
Lock lock = new ReentrantLock();
19+
@Override
20+
public void run() {
21+
// list.add("");
22+
// lock.lock();
23+
// synchronized (this){
24+
// try {
25+
i++;
26+
// }finally {
27+
// lock.unlock();
28+
// }
29+
// }
30+
}
31+
32+
public static void main(String[] args) throws InterruptedException {
33+
for (int j=0;j<20;j++){
34+
// AbstractQueuedSynchronizer;
35+
// Lock;
36+
// ReentrantLock
37+
// HashMap
38+
DemoLock demoLock = new DemoLock();
39+
Thread[] threads = new Thread[1000];
40+
for (int i=0;i<1000;i++){
41+
threads[i] = new Thread(demoLock);
42+
}
43+
for (int i=0;i<1000;i++){
44+
threads[i].start();
45+
}
46+
Thread.sleep(1);
47+
System.out.println(demoLock.i);
48+
}
49+
50+
}
751
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
11
package org.tj.study.thread.locks;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Objects;
6+
import java.util.concurrent.locks.Lock;
7+
import java.util.concurrent.locks.ReadWriteLock;
8+
import java.util.concurrent.locks.ReentrantReadWriteLock;
9+
310
/**
411
* Created by 001 on 16/8/24.
512
*/
613
public class DemoReadWriterLock {
14+
15+
static private Map<String,Object> map = new HashMap<>();
16+
static private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
17+
static private Lock reader = readWriteLock.readLock();
18+
static private Lock writer =readWriteLock.readLock();
19+
20+
static final Object get(String key){
21+
reader.lock();
22+
try {
23+
return map.get(key);
24+
}finally {
25+
reader.unlock();
26+
}
27+
}
28+
29+
static final Object put(String key,Object object){
30+
writer.lock();
31+
try{
32+
map.put(key,object);
33+
}finally {
34+
writer.unlock();
35+
}
36+
return object;
37+
}
38+
39+
static void clear(){
40+
writer.lock();
41+
try {
42+
map.clear();
43+
}finally {
44+
writer.unlock();
45+
}
46+
}
47+
48+
749
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,48 @@
11
package org.tj.study.thread.locks;
22

3+
import java.util.concurrent.locks.Lock;
4+
35
/**
46
* Created by 001 on 16/8/24.
57
*/
68
public class TestTwinsLock {
9+
10+
static Lock lock = new TwinsLock();
11+
12+
13+
public TestTwinsLock(Lock lock) {
14+
this.lock = lock;
15+
}
16+
17+
public static void main(String[] args) throws InterruptedException {
18+
19+
20+
class Worker implements Runnable{
21+
@Override
22+
public void run() {
23+
while (true){
24+
lock.lock();
25+
try {
26+
Thread.sleep(1000);
27+
System.out.print(Thread.currentThread().getName() + " ");
28+
Thread.sleep(1000);
29+
System.out.println();
30+
} catch (InterruptedException e) {
31+
e.printStackTrace();
32+
}finally {
33+
lock.unlock();
34+
}
35+
}
36+
}
37+
}
38+
39+
for (int i = 0; i < 10; i++) {
40+
new Thread(new Worker()).start();
41+
}
42+
//// 每隔1秒换行
43+
// for (int i = 0; i < 10; i++) {
44+
// Thread.sleep(1000);
45+
// System.out.println();
46+
// }
47+
}
748
}

0 commit comments

Comments
 (0)