Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.omsu.imit.emomonov.threads.sending;
- public class Message
- {
- private String sender;
- private String subject;
- private String body;
- private String emailAddress;
- public Message(String sender, String subject, String body, String emailAddress)
- {
- this.sender = sender;
- this.subject = subject;
- this.body = body;
- this.emailAddress = emailAddress;
- }
- public String getSender()
- {
- return sender;
- }
- public String getSubject()
- {
- return subject;
- }
- public String getBody()
- {
- return body;
- }
- public String getEmailAddress()
- {
- return emailAddress;
- }
- public String format()
- {
- return "[" + getSender() + "]: theme=" + getSubject() + "" + "{\n" + getBody() + "\n}";
- }
- @Override
- public String toString()
- {
- return "Message{" +
- "sender='" + sender + '\'' +
- ", subject='" + subject + '\'' +
- ", body='" + body + '\'' +
- ", emailAddress='" + emailAddress + '\'' +
- '}';
- }
- @Override
- public boolean equals(Object o)
- {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Message message = (Message) o;
- if (sender != null ? !sender.equals(message.sender) : message.sender != null) return false;
- if (subject != null ? !subject.equals(message.subject) : message.subject != null) return false;
- if (body != null ? !body.equals(message.body) : message.body != null) return false;
- return emailAddress != null ? emailAddress.equals(message.emailAddress) : message.emailAddress == null;
- }
- @Override
- public int hashCode()
- {
- int result = sender != null ? sender.hashCode() : 0;
- result = 31 * result + (subject != null ? subject.hashCode() : 0);
- result = 31 * result + (body != null ? body.hashCode() : 0);
- result = 31 * result + (emailAddress != null ? emailAddress.hashCode() : 0);
- return result;
- }
- }
- package ru.omsu.imit.emomonov.threads.sending;
- import ru.omsu.imit.emomonov.Constants;
- import ru.omsu.imit.emomonov.threads.date.Formatter;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Date;
- public class Transport
- {
- private String parentDirectory;
- public Transport(String parentDirectory)
- {
- this.parentDirectory = parentDirectory;
- }
- public static void send(Message message)
- {
- new Thread(() -> {
- File file = new File(Constants.PATH_TO_MESSAGE_DIRECTORY, message.getEmailAddress()+".txt");
- try {
- System.out.println(file);
- if (!file.createNewFile()) {
- throw new IOException("Can not create new file ");
- }
- try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
- writer.write(Formatter.format(new Date(), "yyyy.MM.dd G 'at' HH:mm:ss z"));
- writer.write(message.format());
- }
- } catch (IOException e) {
- }
- }).start();
- }
- }
- package ru.omsu.imit.emomonov.threads.task;
- public interface Executable
- {
- boolean isEnd();
- void execute();
- }
- package ru.omsu.imit.emomonov.threads.task;
- import java.util.concurrent.BlockingQueue;
- public class Executor extends Thread
- {
- private BlockingQueue<Executable> queue;
- public Executor(BlockingQueue<Executable> queue)
- {
- this.queue = queue;
- }
- @Override
- public void run()
- {
- boolean exit = true;
- while (exit){
- try {
- Executable executable = queue.take();
- if(executable.isEnd()){
- exit = false;
- }else{
- executable.execute();
- }
- } catch (InterruptedException e) {
- }
- }
- }
- }
- package ru.omsu.imit.emomonov.threads.task;
- import java.util.concurrent.BlockingQueue;
- public class Producer extends Thread
- {
- private BlockingQueue<Executable> queue;
- private int count;
- private String name;
- public Producer(BlockingQueue<Executable> queue, int count, String name)
- {
- this.queue = queue;
- this.count = count;
- this.name = name;
- }
- @Override
- public void run()
- {
- for (int i = 0; i < count; i++) {
- try {
- queue.put(createTask(i));
- } catch (InterruptedException e) {
- }
- }
- }
- private Task createTask(int number)
- {
- return new Task(name + number);
- }
- }
- package ru.omsu.imit.emomonov.threads.task;
- public class Task implements Executable
- {
- private String message;
- public Task(String message)
- {
- this.message = message;
- }
- @Override
- public boolean isEnd()
- {
- return false;
- }
- @Override
- public void execute()
- {
- System.out.println(message);
- }
- }
- package ru.omsu.imit.emomonov.threads.task;
- import com.beust.jcommander.JCommander;
- import com.beust.jcommander.Parameter;
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.BlockingQueue;
- public class TaskOperations
- {
- private static final int NUMBER_ELEMENT_QUEUE = 16;
- @Parameter(names = {"-e", "--executor"})
- private int consumerCount = 1;
- @Parameter(names = {"-p", "--producer"})
- private int producerCount = 1;
- public static void main(String... args)
- {
- TaskOperations taskOperations = new TaskOperations();
- JCommander.newBuilder().addObject(taskOperations).build().parse(args);
- System.out.println("Producer = " + taskOperations.producerCount + " Consumer = " + taskOperations.consumerCount);
- simulateWork(taskOperations.producerCount, taskOperations.consumerCount);
- }
- public static void simulateWork(int countProducer, int countConsumer)
- {
- BlockingQueue<Executable> queue = new ArrayBlockingQueue<>(NUMBER_ELEMENT_QUEUE);
- Producer[] producers = new Producer[countProducer];
- for (int i = 0; i < countProducer; i++) {
- producers[i] = new Producer(queue, 20, "Task");
- producers[i].start();
- }
- for (int i = 0; i < countConsumer; i++) {
- new Executor(queue).start();
- }
- try {
- for (Producer item : producers) {
- item.join();
- }
- } catch (InterruptedException e) {
- }
- for (int i = 0; i < countConsumer; i++) {
- queue.add(new Executable()
- {
- @Override
- public boolean isEnd()
- {
- return true;
- }
- @Override
- public void execute()
- {
- }
- });
- }
- }
- }
- package ru.omsu.imit.emomonov.threads.date;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class Formatter
- {
- private static ThreadLocal<SimpleDateFormat> dateFormat = new ThreadLocal<>();
- public static String format(Date date, String datePattern){
- dateFormat.set(new SimpleDateFormat(datePattern));
- return dateFormat.get().format(date);
- }
- }
- package ru.omsu.imit.emomonov.threads.multitask;
- import com.beust.jcommander.JCommander;
- import com.beust.jcommander.Parameter;
- import ru.omsu.imit.emomonov.threads.task.Executable;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.CountDownLatch;
- public class MultiTaskOperations
- {
- private static final int NUMBER_ELEMENT_QUEUE = 16;
- private static final int NUMBER_STEPS_IN_TASK = 3;
- @Parameter(names = {"-e", "--executor"})
- private int consumerCount = 1;
- @Parameter(names = {"-p", "--producer"})
- private int producerCount = 1;
- private static CountDownLatch countDownLatch;
- private static int count = 0;
- public static void main(String[] args)
- {
- MultiTaskOperations taskOperations = new MultiTaskOperations();
- JCommander.newBuilder().addObject(taskOperations).build().parse(args);
- System.out.println("Producer = " + taskOperations.producerCount + " Consumer = " + taskOperations.consumerCount);
- simulateWork(taskOperations.producerCount, taskOperations.consumerCount);
- }
- public static void simulateWork(int producerCount, int consumerCount)
- {
- BlockingQueue<Task> queue = new ArrayBlockingQueue<>(NUMBER_ELEMENT_QUEUE);
- TaskProducer[] producers = new TaskProducer[producerCount];
- for (int i = 0; i < producerCount; i++) {
- producers[i] = new TaskProducer(queue, NUMBER_STEPS_IN_TASK);
- producers[i].start();
- }
- countDownLatch = new CountDownLatch(NUMBER_STEPS_IN_TASK * producerCount);
- TaskExecutor[] executors = new TaskExecutor[consumerCount];
- for (int i = 0; i < consumerCount; i++) {
- executors[i] = new TaskExecutor(queue, countDownLatch);
- executors[i].start();
- }
- try {
- for (TaskProducer item : producers) {
- item.join();
- }
- } catch (InterruptedException e) {
- }
- try {
- countDownLatch.await();
- } catch (InterruptedException e) {
- }
- for (int i = 0; i < consumerCount; i++) {
- List<Executable> list = new ArrayList<>();
- list.add(new Executable()
- {
- @Override
- public boolean isEnd()
- {
- return true;
- }
- @Override
- public void execute()
- {
- }
- });
- try {
- queue.put(new Task("End", list));
- } catch (InterruptedException e) {
- }
- }
- for (TaskExecutor item : executors) {
- try {
- item.join();
- } catch (InterruptedException e) {
- }
- }
- }
- }
- package ru.omsu.imit.emomonov.threads.multitask;
- import ru.omsu.imit.emomonov.threads.task.Executable;
- import java.util.List;
- public class Task
- {
- private String name;
- private List<Executable> list;
- private int currentTaskNumber;
- public Task(String name, List<Executable> list)
- {
- this.name = name;
- this.list = list;
- this.currentTaskNumber = 0;
- }
- public String getName()
- {
- return name;
- }
- public Executable nextTask()
- {
- return list.get(currentTaskNumber++);
- }
- public int getListSize()
- {
- return list.size();
- }
- public boolean isLast(){
- return currentTaskNumber >= list.size();
- }
- }
- package ru.omsu.imit.emomonov.threads.multitask;
- import ru.omsu.imit.emomonov.threads.task.Executable;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.CountDownLatch;
- public class TaskExecutor extends Thread
- {
- private BlockingQueue<Task> queue;
- private CountDownLatch countDownLatch;
- public TaskExecutor(BlockingQueue<Task> queue, CountDownLatch countDownLatch)
- {
- this.queue = queue;
- this.countDownLatch = countDownLatch;
- }
- @Override
- public void run(){
- while (true){
- try {
- Task task = queue.take();
- System.out.println(task.getName());
- Executable executable = null;
- if(!task.isLast()) {
- executable = task.nextTask();
- if (executable.isEnd()) {
- return;
- } else {
- executable.execute();
- if (!task.isLast()) {
- queue.put(task);
- } else {
- countDownLatch.countDown();
- }
- }
- }
- } catch (InterruptedException e) {
- }
- }
- }
- }
- package ru.omsu.imit.emomonov.threads.multitask;
- import ru.omsu.imit.emomonov.threads.task.Executable;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.CountDownLatch;
- public class TaskExecutor extends Thread
- {
- private BlockingQueue<Task> queue;
- private CountDownLatch countDownLatch;
- public TaskExecutor(BlockingQueue<Task> queue, CountDownLatch countDownLatch)
- {
- this.queue = queue;
- this.countDownLatch = countDownLatch;
- }
- @Override
- public void run(){
- while (true){
- try {
- Task task = queue.take();
- System.out.println(task.getName());
- Executable executable = null;
- if(!task.isLast()) {
- executable = task.nextTask();
- if (executable.isEnd()) {
- return;
- } else {
- executable.execute();
- if (!task.isLast()) {
- queue.put(task);
- } else {
- countDownLatch.countDown();
- }
- }
- }
- } catch (InterruptedException e) {
- }
- }
- }
- }
- package ru.omsu.imit.emomonov.threads.multitask;
- import ru.omsu.imit.emomonov.threads.task.Executable;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.CountDownLatch;
- public class TaskExecutor extends Thread
- {
- private BlockingQueue<Task> queue;
- private CountDownLatch countDownLatch;
- public TaskExecutor(BlockingQueue<Task> queue, CountDownLatch countDownLatch)
- {
- this.queue = queue;
- this.countDownLatch = countDownLatch;
- }
- @Override
- public void run(){
- while (true){
- try {
- Task task = queue.take();
- System.out.println(task.getName());
- Executable executable = null;
- if(!task.isLast()) {
- executable = task.nextTask();
- if (executable.isEnd()) {
- return;
- } else {
- executable.execute();
- if (!task.isLast()) {
- queue.put(task);
- } else {
- countDownLatch.countDown();
- }
- }
- }
- } catch (InterruptedException e) {
- }
- }
- }
- }
- package ru.omsu.imit.emomonov.threads.multitask;
- import ru.omsu.imit.emomonov.threads.task.Executable;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.CountDownLatch;
- public class TaskExecutor extends Thread
- {
- private BlockingQueue<Task> queue;
- private CountDownLatch countDownLatch;
- public TaskExecutor(BlockingQueue<Task> queue, CountDownLatch countDownLatch)
- {
- this.queue = queue;
- this.countDownLatch = countDownLatch;
- }
- @Override
- public void run(){
- while (true){
- try {
- Task task = queue.take();
- System.out.println(task.getName());
- Executable executable = null;
- if(!task.isLast()) {
- executable = task.nextTask();
- if (executable.isEnd()) {
- return;
- } else {
- executable.execute();
- if (!task.isLast()) {
- queue.put(task);
- } else {
- countDownLatch.countDown();
- }
- }
- }
- } catch (InterruptedException e) {
- }
- }
- }
- }
- package ru.omsu.imit.emomonov.threads.multitask;
- import ru.omsu.imit.emomonov.threads.task.Executable;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.CountDownLatch;
- public class TaskExecutor extends Thread
- {
- private BlockingQueue<Task> queue;
- private CountDownLatch countDownLatch;
- public TaskExecutor(BlockingQueue<Task> queue, CountDownLatch countDownLatch)
- {
- this.queue = queue;
- this.countDownLatch = countDownLatch;
- }
- @Override
- public void run(){
- while (true){
- try {
- Task task = queue.take();
- System.out.println(task.getName());
- Executable executable = null;
- if(!task.isLast()) {
- executable = task.nextTask();
- if (executable.isEnd()) {
- return;
- } else {
- executable.execute();
- if (!task.isLast()) {
- queue.put(task);
- } else {
- countDownLatch.countDown();
- }
- }
- }
- } catch (InterruptedException e) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement