首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

线程高级-读写锁

2012-10-07 
线程高级---读写锁??}??catch(InterruptedException ie){?????}?}}???? Reader类。就是将read读入的内容显

线程高级---读写锁
??}
??catch(InterruptedException ie){
???
??}
?}
}

???? Reader类。就是将read读入的内容显示出来。

?

/**
?* 2007-6-19
?* Writer.java
?* package ReadWriteLock
?* TODO As a writer to write data to queue
?* levi
?*/
package ReadWriteLock;

public class Writer extends Thread{
?private Queue queue;
?private String request;
?
?public Writer(Queue queue,String request){
??this.queue = queue;
??this.request = request;
?}
?
?public void run(){
??try{
???queue.write(request);
??}
??catch(InterruptedException ie){
???
??}
?}
}

???? Writer。负责写入一个request

?

/**
?* 2007-6-19
?* Test.java
?* package ReadWriteLock
?* TODO the main thread
?* levi
?*/
package ReadWriteLock;

/**
?* @author levi
?*
?*/
public class Test {
?public static void main(String [] args){
??Queue q = new Queue("* * * * * * * * * *");
??new Writer(q,"I find that I had fallen into you, beautiful girl").start();
??new Writer(q,"But also I am wasting my time,flower becomes air").start();
??new Reader(q).start();
??new Reader(q).start();
??new Reader(q).start();
??new Reader(q).start();
??new Reader(q).start();
??
?}
}

??? 主测试类。主要负责调用Reader和Writer

?

??? 最后,值得提醒的是,类似这样的问题都能抽象出一个共同的特征:那就是读线程比较多或者读操作比较繁重。如果大多数线程是写操作的话,就得评估衡量用读写锁来解决是否合算了。

热点排行