Skip to content

Commit 8904d33

Browse files
author
ymm
committed
2015年12月14日20 提交
1 parent 8146b10 commit 8904d33

4 files changed

Lines changed: 249 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.xiaoyang.thread;
2+
3+
import java.util.concurrent.locks.ReentrantReadWriteLock;
4+
5+
class CachedData {
6+
// 缓存对象
7+
Object data;
8+
// 缓存对象是否有值
9+
volatile boolean cacheValid;
10+
// ReadWirteLock对象
11+
ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
12+
13+
void processCachedData() {
14+
//多个线程来读取数据 上读锁 读锁是可以多个多个线程共同读的 这里加锁的目的在于防止写锁的进入。
15+
rwl.readLock().lock();
16+
//【】【】【】IF A 如果没有数据 进入写数据逻辑
17+
if (!cacheValid) {
18+
// Must release read lock before acquiring write lock
19+
//【】【】【】在写数据之前,先要释放读锁 然后加写锁
20+
rwl.readLock().unlock();
21+
rwl.writeLock().lock();
22+
// Recheck state because another thread might have acquired
23+
// write lock and changed state before we did.
24+
//【】【】【】写数据之前再次检查 缓存释放有数据,多线程下同一时间可能有多个线程进入到第一个IF判断 line17
25+
if (!cacheValid) {
26+
data = "...";
27+
cacheValid = true;
28+
}
29+
// Downgrade by acquiring read lock before releasing write lock
30+
rwl.readLock().lock();
31+
rwl.writeLock().unlock(); // Unlock write, still hold read
32+
}
33+
//ELSE A如果有数据 进入读数据逻辑
34+
// use(data); 伪代码
35+
//读数据结束 释放读锁
36+
rwl.readLock().unlock();
37+
}
38+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.xiaoyang.thread;
2+
import java.util.concurrent.CyclicBarrier;
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
public class CyclicBarrierTest {
7+
8+
public static void main(String[] args) {
9+
ExecutorService service = Executors.newCachedThreadPool();
10+
final CyclicBarrier cb = new CyclicBarrier(5);
11+
for(int i=0;i<5;i++){
12+
Runnable runnable = new Runnable(){
13+
public void run(){
14+
try {
15+
Thread.sleep((long)(Math.random()*10000));
16+
System.out.println("线程" + Thread.currentThread().getName() +
17+
"即将到达集合地点1,当前已有" + (cb.getNumberWaiting()+1) + "个已经到达," + (cb.getNumberWaiting()==4?"都到齐了,继续走啊":"正在等候"));
18+
cb.await();
19+
20+
Thread.sleep((long)(Math.random()*10000));
21+
System.out.println("线程" + Thread.currentThread().getName() +
22+
"即将到达集合地点2,当前已有" + (cb.getNumberWaiting()+1) + "个已经到达," + (cb.getNumberWaiting()==4?"都到齐了,继续走啊":"正在等候"));
23+
cb.await();
24+
Thread.sleep((long)(Math.random()*10000));
25+
System.out.println("线程" + Thread.currentThread().getName() +
26+
"即将到达集合地点3,当前已有" + (cb.getNumberWaiting() + 1) + "个已经到达," + (cb.getNumberWaiting()==4?"都到齐了,继续走啊":"正在等候"));
27+
cb.await();
28+
} catch (Exception e) {
29+
e.printStackTrace();
30+
}
31+
}
32+
};
33+
service.execute(runnable);
34+
}
35+
service.shutdown();
36+
}
37+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.xiaoyang.thread;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/*
7+
*
8+
* Á½¸öÏß³ÌÒÀ´ÎÊä³özhangxiaoxiang lihuoming
9+
* Lock rlock = new ReentrantLock();
10+
* »òÕß
11+
* synchronized
12+
* */
13+
14+
public class OutputTwoStr {
15+
16+
final static Outputer outputer = new Outputer();
17+
public static void main(String[] args) throws InterruptedException {
18+
MyThreadAA a = new MyThreadAA();
19+
MyThreadBB b = new MyThreadBB();
20+
b.start();
21+
a.start();
22+
}
23+
24+
static class Outputer {
25+
Lock rlock = new ReentrantLock();
26+
private void output1(String name) {
27+
rlock.lock();
28+
try {
29+
for (int i = 0; i < name.length(); i++) {
30+
System.out.print(name.charAt(i));
31+
}
32+
System.out.println("");
33+
} catch (Exception e) {
34+
e.printStackTrace();
35+
}finally{
36+
rlock.unlock();
37+
}
38+
39+
}
40+
41+
private void output2(String name) {
42+
rlock.lock();
43+
try {
44+
for (int i = 0; i < name.length(); i++) {
45+
System.out.print(name.charAt(i));
46+
}
47+
System.out.println("");
48+
} catch (Exception e) {
49+
e.printStackTrace();
50+
}finally{
51+
rlock.unlock();
52+
}
53+
}
54+
}
55+
56+
static class MyThreadAA extends Thread {
57+
@Override
58+
public void run() {
59+
while (true) {
60+
try {
61+
outputer.output1("yangnn");
62+
Thread.sleep(20);
63+
} catch (Exception e) {
64+
e.printStackTrace();
65+
}
66+
}
67+
}
68+
}
69+
70+
static class MyThreadBB extends Thread {
71+
@Override
72+
public void run() {
73+
// lock.lock();
74+
while (true) {
75+
try {
76+
outputer.output2("menghh");
77+
Thread.sleep(20);
78+
} catch (Exception e) {
79+
e.printStackTrace();
80+
}
81+
}
82+
// finally{
83+
// lock.unlock();
84+
// }
85+
}
86+
}
87+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.xiaoyang.thread;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/*
7+
*
8+
* Á½¸öÏß³ÌÒÀ´ÎÊä³özhangxiaoxiang lihuoming
9+
* Lock rlock = new ReentrantLock();
10+
* »òÕß
11+
* synchronized
12+
* */
13+
14+
public class ReadWriteLockTest {
15+
16+
final static Outputer outputer = new Outputer();
17+
public static void main(String[] args) throws InterruptedException {
18+
MyThreadAA a = new MyThreadAA();
19+
MyThreadBB b = new MyThreadBB();
20+
b.start();
21+
a.start();
22+
}
23+
24+
static class Outputer {
25+
Lock rlock = new ReentrantLock();
26+
private void output1(String name) {
27+
rlock.lock();
28+
try {
29+
for (int i = 0; i < name.length(); i++) {
30+
System.out.print(name.charAt(i));
31+
}
32+
System.out.println("");
33+
} catch (Exception e) {
34+
e.printStackTrace();
35+
}finally{
36+
rlock.unlock();
37+
}
38+
39+
}
40+
41+
private void output2(String name) {
42+
rlock.lock();
43+
try {
44+
for (int i = 0; i < name.length(); i++) {
45+
System.out.print(name.charAt(i));
46+
}
47+
System.out.println("");
48+
} catch (Exception e) {
49+
e.printStackTrace();
50+
}finally{
51+
rlock.unlock();
52+
}
53+
}
54+
}
55+
56+
static class MyThreadAA extends Thread {
57+
@Override
58+
public void run() {
59+
while (true) {
60+
try {
61+
outputer.output1("yangnn");
62+
Thread.sleep(20);
63+
} catch (Exception e) {
64+
e.printStackTrace();
65+
}
66+
}
67+
}
68+
}
69+
70+
static class MyThreadBB extends Thread {
71+
@Override
72+
public void run() {
73+
// lock.lock();
74+
while (true) {
75+
try {
76+
outputer.output2("menghh");
77+
Thread.sleep(20);
78+
} catch (Exception e) {
79+
e.printStackTrace();
80+
}
81+
}
82+
// finally{
83+
// lock.unlock();
84+
// }
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)