package concurrency;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
class Athlete implements Runnable {
private final int id;
private Game game;
public Athlete(int id, Game game) {
this.id = id;
this.game = game;
}
public boolean equals(Object o) {
if (!(o instanceof Athlete))
return false;
Athlete athlete = (Athlete) o;
return id == athlete.id;
}
public String toString() {
return "Athlete<" + id + ">";
}
public int hashCode() {
return new Integer(id).hashCode();
}
public void run() {
try {
game.prepare(this);
} catch (InterruptedException e) {
System.out.println(this + " quit the game");
}
}
}
public class Game implements Runnable {
private Set<Athlete> players = new HashSet<Athlete>();
private boolean start = false;
public void addPlayer(Athlete one) {
players.add(one);
}
public void removePlayer(Athlete one) {
players.remove(one);
}
public Collection<Athlete> getPlayers() {
return Collections.unmodifiableSet(players);
}
public void prepare(Athlete athlete) throws InterruptedException {
System.out.println(athlete + " ready!");
synchronized (this) {
while (!start)
wait();
if (start)
System.out.println(athlete + " go!");
}
}
public synchronized void go() {
notifyAll();
}
public void ready() {
Iterator<Athlete> iter = getPlayers().iterator();
while (iter.hasNext())
new Thread(iter.next()).start();
}
public void run() {
start = false;
System.out.println("Ready......");
System.out.println("Ready......");
System.out.println("Ready......");
ready();
start = true;
System.out.println("Go!");
go();
}
public static void main(String[] args) {
Game game = new Game();
for (int i = 0; i < 10; i++)
game.addPlayer(new Athlete(i, game));
new Thread(game).start();
}
}
Ready...... Ready...... Ready...... Athlete<0> ready! Athlete<1> ready! Athlete<2> ready! Athlete<3> ready! Athlete<4> ready! Athlete<5> ready! Athlete<6> ready! Athlete<7> ready! Athlete<8> ready! Athlete<9> ready! Go! Athlete<9> go! Athlete<8> go! Athlete<7> go! Athlete<6> go! Athlete<5> go! Athlete<4> go! Athlete<3> go! Athlete<2> go! Athlete<1> go! Athlete<0> go!
import java.util.concurrent.TimeUnit;
class MyObject implements Runnable {
private Monitor monitor;
public MyObject(Monitor monitor) {
this.monitor = monitor;
}
public void run() {
try {
TimeUnit.SECONDS.sleep(3);
System.out.println("i'm going.");
monitor.gotMessage();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Monitor implements Runnable {
private volatile boolean go = false;
public void gotMessage() throws InterruptedException {
go = true;
}
public void watching() {
while (go == false)
;
System.out.println("He has gone.");
}
public void run() {
watching();
}
}
public class BusyWaiting {
public static void main(String[] args) {
Monitor monitor = new Monitor();
MyObject o = new MyObject(monitor);
new Thread(o).start();
new Thread(monitor).start();
}
}
i'm going. He has gone.
package concurrency.wait;
import java.util.concurrent.TimeUnit;
class MyObject implements Runnable {
private Monitor monitor;
public MyObject(Monitor monitor) {
this.monitor = monitor;
}
package java.util.concurrent;
import java.util.*;
public interface Delayed extends Comparable<Delayed> {
long getDelay(TimeUnit unit);
}
package com.zj.timedtask;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class DelayedTasker implements Runnable {
DelayQueue<DelayedTask> queue = new DelayQueue<DelayedTask>();
public void addTask(DelayedTask e) {
queue.put(e);
}
public void removeTask() {
queue.poll();
}
public Collection<DelayedTask> getAllTasks() {
return Collections.unmodifiableCollection(queue);
}
public int getTaskQuantity() {
return queue.size();
}
public void run() {
while (!queue.isEmpty())
try {
queue.take().run();
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("Finished DelayedTask");
}
public static class DelayedTask implements Delayed, Runnable {
private static int counter = 0;
private final int id = counter++;
private final int delta;
private final long trigger;
public DelayedTask(int delayInSeconds) {
delta = delayInSeconds;
trigger = System.nanoTime() + NANOSECONDS.convert(delta, SECONDS);
}
public long getDelay(TimeUnit unit) {
return unit.convert(trigger - System.nanoTime(), NANOSECONDS);
}
public int compareTo(Delayed arg) {
DelayedTask that = (DelayedTask) arg;
if (trigger < that.trigger)
return -1;
if (trigger > that.trigger)
return 1;
return 0;
}
public void run() {
//run all that you want to do
System.out.println(this);
}
public String toString() {
return "[" + delta + "s]" + "Task" + id;
}
}
public static void main(String[] args) {
Random rand = new Random();
ExecutorService exec = Executors.newCachedThreadPool();
DelayedTasker tasker = new DelayedTasker();
for (int i = 0; i < 10; i++)
tasker.addTask(new DelayedTask(rand.nextInt(5)));
exec.execute(tasker);
exec.shutdown();
}
}
[0s]Task 1 [0s]Task 2 [0s]Task 3 [1s]Task 6 [2s]Task 5 [3s]Task 8 [4s]Task 0 [4s]Task 4 [4s]Task 7 [4s]Task 9 Finished DelayedTask
package com.zj.timedtask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ScheduleTasker {
private int corePoolSize = 10;
ScheduledThreadPoolExecutor scheduler;
public ScheduleTasker() {
scheduler = new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduleTasker(int quantity) {
corePoolSize = quantity;
scheduler = new ScheduledThreadPoolExecutor(corePoolSize);
}
public void schedule(Runnable event, long delay) {
scheduler.schedule(event, delay, TimeUnit.SECONDS);
}
public void shutdown() {
scheduler.shutdown();
}
public static void main(String[] args) {
ScheduleTasker tasker = new ScheduleTasker();
tasker.schedule(new Runnable() {
public void run() {
System.out.println("[1s]Task 1");
}
}, 1);
tasker.schedule(new Runnable() {
public void run() {
System.out.println("[2s]Task 2");
}
}, 2);
tasker.schedule(new Runnable() {
public void run() {
System.out.println("[4s]Task 3");
}
}, 4);
tasker.schedule(new Runnable() {
public void run() {
System.out.println("[10s]Task 4");
}
}, 10);
tasker.shutdown();
}
}
[1s]Task 1
[2s]Task 2
[4s]Task 3
[10s]Task 4
public void run() {
try {
TimeUnit.SECONDS.sleep(3);
System.out.println("i'm going.");
monitor.gotMessage();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Monitor implements Runnable {
private volatile boolean go = false;
public synchronized void gotMessage() throws InterruptedException {
go = true;
notify();
}
public synchronized void watching() throws InterruptedException {
while (go == false)
wait();
System.out.println("He has gone.");
}
public void run() {
try {
watching();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Wait {
public static void main(String[] args) {
Monitor monitor = new Monitor();
MyObject o = new MyObject(monitor);
new Thread(o).start();
new Thread(monitor).start();
}
}
i'm going. He has gone.
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有