Skip to content

Instantly share code, notes, and snippets.

@teruteru128
Created June 8, 2016 17:57
Show Gist options
  • Save teruteru128/2edc1c27187350c7e4f1eaa237dfdb5f to your computer and use it in GitHub Desktop.
Save teruteru128/2edc1c27187350c7e4f1eaa237dfdb5f to your computer and use it in GitHub Desktop.
Javaでクソマルチスレッドスケジュールタスク管理試作してみた ref: http://qiita.com/teruteru128/items/1f9dd0689a01cf5b7116
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
public class Main {
/**時刻待ち行列キュー*/
static Queue<SampleBean> queueA = new LinkedBlockingQueue<>();
/**時刻待ち行列書き戻しキュー*/
static Queue<SampleBean> queueB = new LinkedBlockingQueue<>();
/** 実行待ち行列キュー */
static Queue<SampleBean> queueC = new LinkedBlockingQueue<>();
static ExecutorService service = Executors.newWorkStealingPool();
public static void main(String[] args) {
ZonedDateTime nowTime = ZonedDateTime.now();
System.out.println("start : " + nowTime);
ZonedDateTime offseted = nowTime.plus(10, ChronoUnit.SECONDS);
ZonedDateTime nextTime = offseted;
for (int i = 0; i < 10; i++) {
queueA.add(new SampleBean(nextTime, i + 1));
nextTime = nextTime.plusSeconds(2);
}
Runnable a = new SampleThreadA(queueA, queueB, queueC);
Runnable b = new SampleThreadB(queueA, queueB);
Runnable c = new SampleThreadC(queueC);
service.execute(a);
service.execute(b);
service.execute(c);
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finish : " + ZonedDateTime.now());
System.out.println(queueA.size());
System.out.println(queueB.size());
System.out.println(queueC.size());
}
}
import java.io.Serializable;
import java.time.ZonedDateTime;
@SuppressWarnings("serial")
class SampleBean implements Serializable {
long id = 0;
public SampleBean() {
}
public SampleBean(ZonedDateTime time, long id) {
this.time = time;
this.id = id;
message = time.toString() + String.valueOf(id);
}
public ZonedDateTime getTime() {
return time;
}
public void setTime(ZonedDateTime time) {
this.time = time;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
private ZonedDateTime time = null;
private String message = null;
}
import java.time.ZonedDateTime;
import java.util.Queue;
class SampleThreadA implements Runnable {
private Queue<SampleBean> queueA;
private Queue<SampleBean> queueB;
private Queue<SampleBean> queueC;
public SampleThreadA(Queue<SampleBean> queueA, Queue<SampleBean> queueB, Queue<SampleBean> queueC) {
this.queueA = queueA;
this.queueB = queueB;
this.queueC = queueC;
}
@Override
public void run() {
while (true) {
synchronized (queueA) {
ZonedDateTime now = ZonedDateTime.now();
while (!queueA.isEmpty()) {
SampleBean bean = queueA.remove();
ZonedDateTime scheduledTime = bean.getTime();
if (scheduledTime.isEqual(now) || scheduledTime.isBefore(now)) {
synchronized (queueC) {
queueC.add(bean);
}
} else {
queueB.add(bean);
}
}
}
}
}
}
import java.util.Queue;
class SampleThreadB implements Runnable {
private Queue<SampleBean> queueA;
private Queue<SampleBean> queueB;
public SampleThreadB(Queue<SampleBean> queueA, Queue<SampleBean> queueB) {
this.queueA = queueA;
this.queueB = queueB;
}
@Override
public void run() {
while (true) {
synchronized (queueA) {
while (!queueB.isEmpty()) {
SampleBean bean = queueB.remove();
queueA.add(bean);
}
}
}
}
}
import java.time.ZonedDateTime;
import java.util.Queue;
class SampleThreadC implements Runnable {
private Queue<SampleBean> queueC;
public SampleThreadC(Queue<SampleBean> queueC) {
this.queueC = queueC;
}
@Override
public void run() {
while (true) {
synchronized (queueC) {
while (!queueC.isEmpty()) {
SampleBean bean = queueC.remove();
System.out.println(bean.getMessage());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment