Пятница, 2025-02-07, 23:49:58
Главная Регистрация RSS
Приветствую Вас, Гость
[ Новые сообщения · Участники · Правила форума · Поиск · RSS ]
  • Страница 1 из 1
  • 1
Синхронизация потоков
CHerryДата: Среда, 2011-10-26, 20:52:51 | Сообщение # 1
Генералиссимус
Группа: Администраторы
Сообщений: 141
Репутация: 3732
Статус: Offline
Code

// ScheduledExecutorService

public class CallableImpl implements Callable<Integer> {
public Integer call() {
//пїЅ
return new Integer(someValue);  
}  
}
//пїЅ
Callable<Integer> callable = new CallableImpl();
ExecutorService executor = Executors.newFixedThreadPool(5);
Future<Integer> future = executor.submit(callable);
try {
System.out.println( "Future value: " + future.get());
} catch (Exception e) {
e.printStackTrace();  
}

// Semaphores

private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);

public Object getItem() throws InterruptedException {
available.acquire();
return getNextAvailableItem();  
}

public void putItem(Object x) {
if (markAsUnused(x))
available.release();  
}

// CyclicBarrier

class Worker extends Thread {
//пїЅ
@Override  
public void run() {  
// пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ  
try {  
barrier.await();  
} catch (InterruptedException e) {  
e.printStackTrace();  
} catch (BrokenBarrierException e) {  
e.printStackTrace();  
}  
}  
}  
//пїЅ
barrier = new CyclicBarrier(N, new Runnable() {
public void run() {  
// пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ, пїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ  
}  
});  
for (int i = 0; i < N; i++) {
new Worker(barrier).start();  
}

// Exchanger

Exchanger<MyClass> exchanger = new Exchanger<MyClass>();  
class Loop1 implements Runnable {
public void run() {  
MyClass loop1Value = пїЅ  
loop1Value = exchanger.exchange(loop1Value );  
//пїЅ
}  
}
class Loop2 implements Runnable {
public void run() {  
MyClass loop2Value = пїЅ  
loop2Value = exchanger.exchange(loop2Value );  
//пїЅ
}  
}

// CountDownLatch

public class LatchedThread extends Thread {
private final CountDownLatch startLatch;  
public LatchedThread(CountDownLatch startLatch) {  
this.startLatch = startLatch;  
}  
public void run() {  
System.out.println( "PreRun");
try {  
startLatch.await();  
System.out.println( "Run");
} catch (InterruptedException iex) {  
}  
}  
}
//пїЅ
CountDownLatch startLatch = new CountDownLatch(1);
for (int threadNo = 0; threadNo < 4; threadNo++) {
Thread t = new LatchedThread(startLatch);  
t.start();  
}
Thread.sleep(200);
startLatch.countDown();

public class StopLatchedThread extends Thread {
private final CountDownLatch stopLatch;  
public StopLatchedThread(CountDownLatch stopLatch) {  
this.stopLatch = stopLatch;  
}  
public void run() {  
try {  
// пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ  
} finally {  
stopLatch.countDown();  
}  
}  
}

CountDownLatch cdl = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
Thread t = new StopLatchedThread(cdl);  
t.start();  
}
cdl.await();
System.out.println("Stopped");

// Lock

Lock l = ...;  
l.lock();  
try {  
// пїЅпїЅпїЅпїЅпїЅпїЅ пїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ  
} finally {  
l.unlock();  
}  

// ReentrantReadWriteLock

public void writeData(String str) {
lock.writeLock().lock();  
//Write data  
lock.writeLock().unlock();  
}

public void readData() {
lock.readLock().lock();
//Read data  
lock.readLock().unlock();  
}

// Condition

public interface Condition {
void await() throws InterruptedException;  
void awaitUninterruptibly();  
long awaitNanos(long nanosTimeout) throws     InterruptedException;  
boolean await(long time, TimeUnit unit) throws InterruptedException;  
boolean awaitUntil(Date deadline) throws InterruptedException;  
void signal();  
void signalAll();  
}

+

final Condition notFull  = lock.newCondition();  
final Condition notEmpty = lock.newCondition();

 
  • Страница 1 из 1
  • 1
Поиск: