Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating and Running a Daemon Thread
import java.util.Date;
import java.util.Deque;
public class CleanerTask extends Thread {
private Deque<Event> deque;
public CleanerTask(Deque<Event> deque) {
this.deque = deque;
// Establish that this is a Daemon Thread
setDaemon(true);
}
@Override
public void run() {
while (true) {
Date date = new Date();
clean(date);
}
}
private void clean(Date date) {
long difference;
boolean delete;
if (deque.size() == 0) {
return;
}
delete = false;
do {
Event e = deque.getLast();
difference = date.getTime() - e.getDate().getTime();
if (difference > 10000) {
System.out.printf("Cleaner: %s\n", e.getEvent());
deque.removeLast();
delete = true;
}
} while (difference > 10000);
if (delete) {
System.out.printf("Cleaner: Size of the queue: %d\n", deque.size());
}
}
}
import java.util.Date;
public class Event {
private Date date;
private String event;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getEvent() {
return event;
}
public void setEvent(String event) {
this.event = event;
}
}
// Creating and running a daemon thread
import java.util.Deque;
import java.util.concurrent.ConcurrentLinkedDeque;
public class Main {
public static void main(String[] args) {
// Creates the Event data structure
Deque<Event> deque = new ConcurrentLinkedDeque<>();
// Creates the three WriterTask and starts them
WriterTask writer = new WriterTask(deque);
for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
Thread thread = new Thread(writer);
thread.start();
}
// Creates a cleaner task and starts them
CleanerTask cleaner = new CleanerTask(deque);
cleaner.start();
}
}
import java.util.Date;
import java.util.Deque;
import java.util.concurrent.TimeUnit;
public class WriterTask implements Runnable {
Deque<Event> deque;
public WriterTask(Deque<Event> deque) {
this.deque = deque;
}
@Override
public void run() {
// Writes 100 events
for (int i = 1; i < 100; i++) {
// Creates and initializes the Event objects
Event event = new Event();
event.setDate(new Date());
event.setEvent(
String.format("The thread %s has generated the event %d event", Thread.currentThread().getId(), i));
// Add to the data structure
deque.addFirst(event);
try {
// Sleeps during one second
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment