多线程并发线程的读写锁

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/iteye_10645/article/details/82671527


/*
读写锁,读读不互斥,读写互斥,写写互斥
互斥的放在同一个类中 */

import java.util.Random;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
publicclass ReadWriteLockTest {
publicstaticvoid main(String[] args) {
final Queue3 q3 = new Queue3();
for(inti=0;i<3;i++)
{
new Thread(){
publicvoid run(){
while(true){
q3.get();
}
}
}.start();

new Thread(){
publicvoid run(){
while(true){
q3.put(new Random().nextInt(10000));
}
}
}.start();
}
}
}

class Queue3{
private Object data = null;//共享数据,只能有一个线程能写该数据,但可以有多个线程同时读该数据。
ReadWriteLock rwl = new ReentrantReadWriteLock();

publicvoid get(){
rwl.readLock().lock();
try{
System.out.println(Thread.currentThread().getName() + " be ready to read data!");
Thread.sleep((long)(Math.random()*1000));
System.out.println(Thread.currentThread().getName() + "have read data :" + data);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
rwl.readLock().unlock();
}
}

publicvoid put(Object data){
rwl.writeLock().lock();
try{
System.out.println(Thread.currentThread().getName() + " be ready to write data!");
Thread.sleep((long)(Math.random()*1000));
this.data = data;
System.out.println(Thread.currentThread().getName() + " have write data: " + data);
}catch (InterruptedException e){
e.printStackTrace();
}finally{
rwl.writeLock().unlock();
}
}
}


读写锁的cache
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class CacheDemo {
private Map<String, Object> cache = new HashMap<String, Object>();
public static void main(String[] args) {

}
private ReadWriteLock rwl = new ReentrantReadWriteLock();
public Object getData(String key){
rwl.readLock().lock();
Object value = null;
try{
value = cache.get(key);
if(value == null){
rwl.readLock( ).unlock();
rwl.writeLock().lock();
try{
if(value==null){
value = "aaaa";//实际失去queryDB();
}
}finally{
rwl.writeLock().unlock();
}
rwl.readLock().lock();
}
}finally{
rwl.readLock().unlock();
}
return value;
}
}


展开阅读全文

线程读写的问题

11-13

代码如下:rn[root@localhost 11ÕÂ]# cat -n pthread_rwlock_example.crn 1 #include rn 2 #include rn 3 #include rn 4 #include rn 5 #include rn 6rn 7 static pthread_rwlock_t rwlock;rn 8rn 9 #define WORK_SIZE 1024rn 10 char work_area[WORK_SIZE];rn 11 int time_to_exit;rn 12rn 13 void *thread_function_read_o(void *arg);rn 14 void *thread_function_read_t(void *arg);rn 15 void *thread_function_write_o(void *arg);rn 16 void *thread_function_write_t(void *arg);rn 17rn 18rn 19 int main(int argc,char *argv[])rn 20 rn 21 int res;rn 22 pthread_t a_thread,b_thread,c_thread,d_thread;rn 23 void *thread_result;rn 24rn 25 res=pthread_rwlock_init(&rwlock,NULL);rn 26 if (res != 0)rn 27 rn 28 perror("rwlock initialization failed");rn 29 exit(EXIT_FAILURE);rn 30 rn 31 res = pthread_create(&a_thread, NULL, thread_function_read_o, NULL);//create new threadrn 32 if (res != 0)rn 33 rn 34 perror("Thread creation failed");rn 35 exit(EXIT_FAILURE);rn 36 rn 37rn 38 res = pthread_create(&b_thread, NULL, thread_function_read_t, NULL);//create new threadrn 39 if (res != 0)rn 40 rn 41 perror("Thread creation failed");rn 42 exit(EXIT_FAILURE);rn 43 rn 44 res = pthread_create(&c_thread, NULL, thread_function_write_o, NULL);//create new threadrn 45 if (res != 0)rn 46 rn 47 perror("Thread creation failed");rn 48 exit(EXIT_FAILURE);rn 49 rn 50 res = pthread_create(&d_thread, NULL, thread_function_write_t, NULL);//create new threadrn 51 if (res != 0)rn 52 rn 53 perror("Thread creation failed");rn 54 exit(EXIT_FAILURE);rn 55 rn 56rn 57 res = pthread_join(a_thread, &thread_result);rn 58 if (res != 0)rn 59 rn 60 perror("Thread join failed");rn 61 exit(EXIT_FAILURE);rn 62 rn 63 res = pthread_join(b_thread, &thread_result);rn 64 if (res != 0)rn 65 rn 66 perror("Thread join failed");rn 67 exit(EXIT_FAILURE);rn 68 rn 69 res = pthread_join(c_thread, &thread_result);rn 70 if (res != 0)rn 71 rn 72 perror("Thread join failed");rn 73 exit(EXIT_FAILURE);rn 74 rn 75 res = pthread_join(d_thread, &thread_result);rn 76 if (res != 0)rn 77 rn 78 perror("Thread join failed");rn 79 exit(EXIT_FAILURE);rn 80 rn 81rn 82 pthread_rwlock_destroy(&rwlock);rn 83 exit(EXIT_SUCCESS);rn 84 rn 85rn 86 void *thread_function_read_o(void *arg)rn 87 rn 88 printf("thread read one try to get lock\n");rn 89rn 90 pthread_rwlock_rdlock(&rwlock);rn 91 while(strncmp("end", work_area, 3) != 0)rn 92 rn 93 printf("this is thread read one.");rn 94 printf("the characters is %s",work_area);rn 95 pthread_rwlock_unlock(&rwlock);rn 96 sleep(2);rn 97 pthread_rwlock_rdlock(&rwlock);rn 98 while (work_area[0] == '\0' )rn 99 rn 100 pthread_rwlock_unlock(&rwlock);rn 101 sleep(2);rn 102 pthread_rwlock_rdlock(&rwlock);rn 103 rn 104 rn 105 pthread_rwlock_unlock(&rwlock);rn 106 time_to_exit=1;rn 107 pthread_exit(0);rn 108 rn 109rn 110 void *thread_function_read_t(void *arg)rn 111 rn 112 printf("thread read one try to get lock\n");rn 113 pthread_rwlock_rdlock(&rwlock);rn 114 while(strncmp("end", work_area, 3) != 0)rn 115 rn 116 printf("this is thread read two.");rn 117 printf("the characters is %s",work_area);rn 118 pthread_rwlock_unlock(&rwlock);rn 119 sleep(5);rn 120 pthread_rwlock_rdlock(&rwlock);rn 121 while (work_area[0] == '\0' )rn 122 rn 123 pthread_rwlock_unlock(&rwlock);rn 124 sleep(5);rn 125 pthread_rwlock_rdlock(&rwlock);rn 126 rn 127 rn 128 pthread_rwlock_unlock(&rwlock);rn 129 time_to_exit=1;rn 130 pthread_exit(0);rn 131 rn 132rn 133 void *thread_function_write_o(void *arg)rn 134 rn 135 printf("this is write thread one try to get lock\n");rn 136 while(!time_to_exit)rn 137 rn 138 pthread_rwlock_wrlock(&rwlock);rn 139 printf("this is write thread one.\nInput some text. Enter 'end' to finish\n");rn 140 fgets(work_area, WORK_SIZE, stdin);rn 141 pthread_rwlock_unlock(&rwlock);rn 142 sleep(15);rn 143 rn 144 pthread_rwlock_unlock(&rwlock);rn 145 pthread_exit(0);rn 146 rn 147rn 148 void *thread_function_write_t(void *arg)rn 149 rn 150 sleep(10);rn 151 while(!time_to_exit)rn 152 rn 153 pthread_rwlock_wrlock(&rwlock);rn 154 printf("this is write thread two.\nInput some text. Enter 'end' to finish\n");rn 155 fgets(work_area, WORK_SIZE, stdin);rn 156 pthread_rwlock_unlock(&rwlock);rn 157 sleep(20);rn 158 rn 159 pthread_rwlock_unlock(&rwlock);rn 160 pthread_exit(0);rn 161 rn编译运行:rn[root@localhost 11ÕÂ]# gcc -o pthread_rwlock_example -D_GUN_SOURCE pthread_rwlock_example.c -lpthreadrnpthread_rwlock_example.c:7: parse error before "rwlock"rnpthread_rwlock_example.c:7: warning: data definition has no type or storage classrn问题:rn我查了好多次,也没有发现第七行的问题,请高手指教!不胜感激!rn我的内核版本:rn[root@localhost proc]# cat versionrnLinux version 2.4.20-31.9 (bhcompile@daffy.perf.redhat.com) (gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)) #1 Tue Apr 13 18:04:23 EDT 2004rn难道与内核版本或者gcc版本有关?请高手指教!不胜感激!rn 论坛

没有更多推荐了,返回首页