《liNUX实战:各种同步控制工具的使用》要点:
本文介绍了liNUX实战:各种同步控制工具的使用,希望对您有用。如果有疑问,可以联系我们。
共享锁,运行多个线程同时临界区
主要接口public voID acquire()
public voID acquireUninterruptibly()
public boolean tryAcquire()
public boolean tryAcquire(long timeout,TimeUnit unit)
public voID release()
public
class
SemaphoreDemo {
private
static
final
int
THREAD_COUNT = 3;
private
static
ExecutorService threadPool = Executors
.newFixedThreadPool(THREAD_COUNT);
private
static
Semaphore s =
new
Semaphore(1);
public
static
voID
main(String[] args) {
for
(
int
i=0;i<3;i++)
{
threadPool.execute(
new
Runnable() {
@OverrIDe
public
voID
run() {
try
{
System.
out
.println(Thread.currentThread().getname()+
"start data"
);
Thread.sleep(2000);
s.acquire();
Thread.sleep(1000);
System.
out
.println(Thread.currentThread().getname()+
"save data"
);
s.release();
System.
out
.println(Thread.currentThread().getname()+
"release data"
);
Thread.sleep(2000);
System.
out
.println(Thread.currentThread().getname()+
"end data"
);
}
catch
(InterruptedException e) {
e.printstacktrace();
}
}
});
}
threadPool.shutdown();
}
}
最后得出一个比较有意思的结论:Semaphore 像是一个共享的屋子,这个屋子里面只能有一定的人数,这个人数是所有人可以看到的,甚至与release()这个办法,可以被别的线程进行调用,
一般使用acquire() 与release() 这个之间的代码只能有固定数量的线程存在,当然这种是当火线程进行获取和释放
ReaDWriteLock 概述ReaDWriteLock是JDK5中提供的读写分别锁
private static @R_419_289@ reaDWriteLock=new @R_419_289@();
private static Lock readLock = reaDWriteLock.readLock();
private static Lock writeLock = reaDWriteLock.writeLock();
这个类如果没有写锁的情况下,读是无阻塞的,在必定程度上提高了程序的执行效率.
public
voID
run() {
//isRead自界说变量(判断这个线程是读还是写)
if
(isRead) {
//获取读锁
myLock.readLock().
lock
();
System.
out
.println(
"读"
);
//释放读锁
myLock.readLock().unlock();
}
else
{
//获取写锁
myLock.writeLock().
lock
();
//执行现金业务
System.
out
.println(
"写"
);
//释放写锁
myLock.writeLock().unlock();
}
}
CountDownLatch 概述:
static final CountDownLatch end = new CountDownLatch(10);
end.countDown(); //这个办法是子线程作完作业之后,调用的
end.await(); //主线等待指定数量的子线程完成作业,当所有子线程完成之后,主线程自动激活执行
public
class
CountDownLatchDemo {
private
static
CountDownLatch countDownLatch=
new
CountDownLatch(10);
public
static
voID
main(String[] args) {
for
(
int
i=0;i<10;i++)
{
new
Thread(
new
Runnable() {
@OverrIDe
public
voID
run() {
System.
out
.println(Thread.currentThread().getname()+
"work"
);
try
{
Thread.sleep(2000);
}
catch
(InterruptedException e) {
e.printstacktrace();
}
countDownLatch.countDown();
}
}).start();
}
new
Thread(
new
Runnable() {
@OverrIDe
public
voID
run() {
try
{
countDownLatch.await();
System.
out
.println(Thread.currentThread().getname()+
"主线程start"
);
}
catch
(InterruptedException e) {
e.printstacktrace();
}
}
},
"main1"
).start();
}
}
CyclicbarrIEr 概述
public
class
CyclicbarrIErDemo {
public
static
class
SoldIEr implements Runnable{
private
String name;
private
final CyclicbarrIEr cyclicbarrIEr;
public
SoldIEr(String name,CyclicbarrIEr c) {
this
.name = name;
this
.cyclicbarrIEr=c;
}
@OverrIDe
public
voID
run() {
try
{
//等待所有士兵到齐
System.
out
.println(name +
"报道"
);
cyclicbarrIEr.await();
dowork();
//等待所有士兵完成工作
cyclicbarrIEr.await();
}
catch
(Exception e)
{
e.printstacktrace();
}
}
public
voID
dowork()
{
System.
out
.println(name +
"完成任务"
);
}
}
public
static
class
barrIErRun implements Runnable{
boolean flag;
int
number;
public
barrIErRun(boolean flag,
int
number) {
this
.flag = flag;
this
.number = number;
}
@OverrIDe
public
voID
run() {
if
(!flag)
{
System.
out
.println(
"士兵集合完毕"
);
flag=
true
;
System.
out
.println(
"开始执行任务"
);
}
else
{
System.
out
.println(
"任务完成"
);
}
}
}
public
static
voID
main(String[] args) {
final
int
N =10;
CyclicbarrIEr barrIEr =
new
CyclicbarrIEr(N,
new
barrIErRun(
false
,N));
System.
out
.println(
"集合队伍"
);
for
(
int
i=0;i<N;i++)
{
new
Thread(
new
SoldIEr(
"士兵"
+i,barrIEr)).start();
}
}
}
每次CyclicbarrIEr 调用await()办法之后,都会等待所有的子线程,之后执行CyclicbarrIEr 的Runnable的办法
LockSupport 概述unpark函数可以先于park调用.好比线程B调用unpark函数,给线程A发了一个“许可”,那么当线程A调用park时,它发现已经有“许可”了,那么它会马上再继续运行.
park和unpark的灵活之处上面已经提到,unpark函数可以先于park挪用,这个正是它们的灵活之处.
一个线程它有可能在别的线程unPark之前,或者之后,或者同时调用了park,那么因为park的特性,它可以不用担心本身的park的时序问题
本文永远更新链接地址:
欢迎参与《liNUX实战:各种同步控制工具的使用》讨论,分享您的想法,内存溢出PHP学院为您提供专业教程。
总结以上是内存溢出为你收集整理的LINUX实战:各种同步控制工具的使用全部内容,希望文章能够帮你解决LINUX实战:各种同步控制工具的使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)