Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active December 19, 2021 10:30
Show Gist options
  • Save sunmeat/0372b5825a2f2a1510cfca0c64cbcc3c to your computer and use it in GitHub Desktop.
Save sunmeat/0372b5825a2f2a1510cfca0c64cbcc3c to your computer and use it in GitHub Desktop.
example of queue
package collections;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
class Program {
public static void main(String[] args) throws InterruptedException, IOException {
Random rnd = new Random();
Printer testprinter = new Printer("HP LaserJet 9050DN");
int numberOfClients = 5;
Client[] clients = new Client[numberOfClients];
{
for (int i = 0; i < numberOfClients; i++) {
String name = "User-";
for (int j = 0; j < 3; j++) {
name = name.concat(Integer.toString(rnd.nextInt(9)));
}
clients[i] = new Client(name, testprinter);
System.out.println(clients[i].getName() + " start working.");
}
System.out.println();
}
PrintQueue printQueue = new PrintQueue(testprinter.priorityQueue);
Thread print = new Thread(printQueue, "Print thread");
print.start();
while (true) {
Thread.sleep((rnd.nextInt(3) + 3) * 1000);
clients[rnd.nextInt(numberOfClients)].printNewDocument();
}
}
}
class PrintQueue implements Runnable {
BlockingQueue<Document> queue;
PrintQueue(BlockingQueue<Document> queue) {
this.queue = queue;
}
public void run() {
Random rnd = new Random();
while (true) {
if (queue.size() > 0) {
Document doc = queue.poll();
System.out.println("Document \"" + doc.getName() + "\" start printing.");
try {
Thread.sleep(doc.getPages() * 3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Document \"" + doc.getName() + "\" finish printing.\n");
} else {
try {
Thread.sleep((rnd.nextInt(3) + 3) * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class Printer {
String printerName;
BlockingQueue<Document> priorityQueue = new PriorityBlockingQueue<Document>();
Printer(String name) {
printerName = name;
System.out.println("Printer " + printerName + " is ON.");
}
public void showQueue() {
for (Document doc : priorityQueue) {
System.out.println(doc + " by " + doc.client.getName());
}
System.out.println("--------------");
}
}
class Document implements Comparable<Document> {
private String documentName;
private int pages;
Client client;
Random rnd = new Random();
public Document(Client client) {
this.client = client;
documentName = randomName();
pages = rnd.nextInt(3);
}
private String randomName() {
char[] ch = new char[10];
ch[0] = (char) (rnd.nextInt(26) + 64);
for (int i = 1; i < 6; i++) {
ch[i] = (char) (rnd.nextInt(26) + 97);
}
ch[6] = '-';
for (int i = 7; i < 10; i++) {
ch[i] = (char) (rnd.nextInt(9) + 48);
}
return new String(ch);
}
String getName() {
return documentName;
}
int getPages() {
return pages;
}
public String toString() {
return documentName;
}
public int compareTo(Document doc) {
int myPriority = Integer.parseInt(this.client.getName().substring(5));
int hisPriority = Integer.parseInt(doc.client.getName().substring(5));
return myPriority - hisPriority;
}
}
class Client {
private String clientName;
Printer myPrinter;
public Client(String name, Printer printer) {
clientName = name;
myPrinter = printer;
}
String getName() {
return clientName;
}
public void printNewDocument() throws InterruptedException {
Document doc = new Document(this);
myPrinter.priorityQueue.add(doc);
System.out.println("New document \"" + doc.getName() + "\" was sent to printer queue by " + clientName + ".");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment