Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.53 KB | None | 0 0
  1. package ru.omsu.imit.emomonov.threads.sending;
  2.  
  3. public class Message
  4. {
  5. private String sender;
  6. private String subject;
  7. private String body;
  8. private String emailAddress;
  9.  
  10. public Message(String sender, String subject, String body, String emailAddress)
  11. {
  12. this.sender = sender;
  13. this.subject = subject;
  14. this.body = body;
  15. this.emailAddress = emailAddress;
  16. }
  17.  
  18. public String getSender()
  19. {
  20. return sender;
  21. }
  22.  
  23. public String getSubject()
  24. {
  25. return subject;
  26. }
  27.  
  28. public String getBody()
  29. {
  30. return body;
  31. }
  32.  
  33. public String getEmailAddress()
  34. {
  35. return emailAddress;
  36. }
  37.  
  38. public String format()
  39. {
  40. return "[" + getSender() + "]: theme=" + getSubject() + "" + "{\n" + getBody() + "\n}";
  41. }
  42.  
  43. @Override
  44. public String toString()
  45. {
  46. return "Message{" +
  47. "sender='" + sender + '\'' +
  48. ", subject='" + subject + '\'' +
  49. ", body='" + body + '\'' +
  50. ", emailAddress='" + emailAddress + '\'' +
  51. '}';
  52. }
  53.  
  54. @Override
  55. public boolean equals(Object o)
  56. {
  57. if (this == o) return true;
  58. if (o == null || getClass() != o.getClass()) return false;
  59.  
  60. Message message = (Message) o;
  61.  
  62. if (sender != null ? !sender.equals(message.sender) : message.sender != null) return false;
  63. if (subject != null ? !subject.equals(message.subject) : message.subject != null) return false;
  64. if (body != null ? !body.equals(message.body) : message.body != null) return false;
  65. return emailAddress != null ? emailAddress.equals(message.emailAddress) : message.emailAddress == null;
  66. }
  67.  
  68. @Override
  69. public int hashCode()
  70. {
  71. int result = sender != null ? sender.hashCode() : 0;
  72. result = 31 * result + (subject != null ? subject.hashCode() : 0);
  73. result = 31 * result + (body != null ? body.hashCode() : 0);
  74. result = 31 * result + (emailAddress != null ? emailAddress.hashCode() : 0);
  75. return result;
  76. }
  77. }
  78.  
  79.  
  80. package ru.omsu.imit.emomonov.threads.sending;
  81.  
  82. import ru.omsu.imit.emomonov.Constants;
  83. import ru.omsu.imit.emomonov.threads.date.Formatter;
  84.  
  85. import java.io.BufferedWriter;
  86. import java.io.File;
  87. import java.io.FileWriter;
  88. import java.io.IOException;
  89. import java.util.Date;
  90.  
  91. public class Transport
  92. {
  93. private String parentDirectory;
  94.  
  95. public Transport(String parentDirectory)
  96. {
  97. this.parentDirectory = parentDirectory;
  98. }
  99.  
  100. public static void send(Message message)
  101. {
  102. new Thread(() -> {
  103. File file = new File(Constants.PATH_TO_MESSAGE_DIRECTORY, message.getEmailAddress()+".txt");
  104. try {
  105. System.out.println(file);
  106. if (!file.createNewFile()) {
  107. throw new IOException("Can not create new file ");
  108. }
  109. try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
  110. writer.write(Formatter.format(new Date(), "yyyy.MM.dd G 'at' HH:mm:ss z"));
  111. writer.write(message.format());
  112. }
  113. } catch (IOException e) {
  114. }
  115. }).start();
  116. }
  117. }
  118.  
  119. package ru.omsu.imit.emomonov.threads.task;
  120.  
  121. public interface Executable
  122. {
  123. boolean isEnd();
  124. void execute();
  125. }
  126.  
  127. package ru.omsu.imit.emomonov.threads.task;
  128.  
  129. import java.util.concurrent.BlockingQueue;
  130.  
  131. public class Executor extends Thread
  132. {
  133. private BlockingQueue<Executable> queue;
  134.  
  135. public Executor(BlockingQueue<Executable> queue)
  136. {
  137. this.queue = queue;
  138. }
  139.  
  140. @Override
  141. public void run()
  142. {
  143. boolean exit = true;
  144. while (exit){
  145. try {
  146. Executable executable = queue.take();
  147. if(executable.isEnd()){
  148. exit = false;
  149. }else{
  150. executable.execute();
  151. }
  152. } catch (InterruptedException e) {
  153. }
  154. }
  155. }
  156. }
  157.  
  158.  
  159. package ru.omsu.imit.emomonov.threads.task;
  160.  
  161. import java.util.concurrent.BlockingQueue;
  162.  
  163. public class Producer extends Thread
  164. {
  165. private BlockingQueue<Executable> queue;
  166. private int count;
  167. private String name;
  168.  
  169. public Producer(BlockingQueue<Executable> queue, int count, String name)
  170. {
  171. this.queue = queue;
  172. this.count = count;
  173. this.name = name;
  174. }
  175.  
  176. @Override
  177. public void run()
  178. {
  179. for (int i = 0; i < count; i++) {
  180. try {
  181. queue.put(createTask(i));
  182. } catch (InterruptedException e) {
  183. }
  184. }
  185. }
  186.  
  187. private Task createTask(int number)
  188. {
  189. return new Task(name + number);
  190. }
  191. }
  192.  
  193.  
  194. package ru.omsu.imit.emomonov.threads.task;
  195.  
  196. public class Task implements Executable
  197. {
  198. private String message;
  199.  
  200. public Task(String message)
  201. {
  202. this.message = message;
  203. }
  204.  
  205. @Override
  206. public boolean isEnd()
  207. {
  208. return false;
  209. }
  210.  
  211. @Override
  212. public void execute()
  213. {
  214. System.out.println(message);
  215. }
  216. }
  217.  
  218.  
  219. package ru.omsu.imit.emomonov.threads.task;
  220.  
  221. import com.beust.jcommander.JCommander;
  222. import com.beust.jcommander.Parameter;
  223. import java.util.concurrent.ArrayBlockingQueue;
  224. import java.util.concurrent.BlockingQueue;
  225.  
  226. public class TaskOperations
  227. {
  228. private static final int NUMBER_ELEMENT_QUEUE = 16;
  229. @Parameter(names = {"-e", "--executor"})
  230. private int consumerCount = 1;
  231. @Parameter(names = {"-p", "--producer"})
  232. private int producerCount = 1;
  233.  
  234. public static void main(String... args)
  235. {
  236. TaskOperations taskOperations = new TaskOperations();
  237. JCommander.newBuilder().addObject(taskOperations).build().parse(args);
  238. System.out.println("Producer = " + taskOperations.producerCount + " Consumer = " + taskOperations.consumerCount);
  239. simulateWork(taskOperations.producerCount, taskOperations.consumerCount);
  240. }
  241.  
  242. public static void simulateWork(int countProducer, int countConsumer)
  243. {
  244. BlockingQueue<Executable> queue = new ArrayBlockingQueue<>(NUMBER_ELEMENT_QUEUE);
  245. Producer[] producers = new Producer[countProducer];
  246. for (int i = 0; i < countProducer; i++) {
  247. producers[i] = new Producer(queue, 20, "Task");
  248. producers[i].start();
  249. }
  250. for (int i = 0; i < countConsumer; i++) {
  251. new Executor(queue).start();
  252. }
  253. try {
  254. for (Producer item : producers) {
  255. item.join();
  256. }
  257. } catch (InterruptedException e) {
  258. }
  259. for (int i = 0; i < countConsumer; i++) {
  260. queue.add(new Executable()
  261. {
  262. @Override
  263. public boolean isEnd()
  264. {
  265. return true;
  266. }
  267.  
  268. @Override
  269. public void execute()
  270. {
  271.  
  272. }
  273. });
  274. }
  275. }
  276. }
  277. package ru.omsu.imit.emomonov.threads.date;
  278.  
  279. import java.text.SimpleDateFormat;
  280. import java.util.Date;
  281.  
  282. public class Formatter
  283. {
  284. private static ThreadLocal<SimpleDateFormat> dateFormat = new ThreadLocal<>();
  285. public static String format(Date date, String datePattern){
  286. dateFormat.set(new SimpleDateFormat(datePattern));
  287. return dateFormat.get().format(date);
  288. }
  289. }
  290.  
  291.  
  292. package ru.omsu.imit.emomonov.threads.multitask;
  293.  
  294. import com.beust.jcommander.JCommander;
  295. import com.beust.jcommander.Parameter;
  296. import ru.omsu.imit.emomonov.threads.task.Executable;
  297.  
  298. import java.util.ArrayList;
  299. import java.util.List;
  300. import java.util.concurrent.ArrayBlockingQueue;
  301. import java.util.concurrent.BlockingQueue;
  302. import java.util.concurrent.CountDownLatch;
  303.  
  304. public class MultiTaskOperations
  305. {
  306. private static final int NUMBER_ELEMENT_QUEUE = 16;
  307. private static final int NUMBER_STEPS_IN_TASK = 3;
  308. @Parameter(names = {"-e", "--executor"})
  309. private int consumerCount = 1;
  310. @Parameter(names = {"-p", "--producer"})
  311. private int producerCount = 1;
  312. private static CountDownLatch countDownLatch;
  313.  
  314. private static int count = 0;
  315.  
  316. public static void main(String[] args)
  317. {
  318. MultiTaskOperations taskOperations = new MultiTaskOperations();
  319. JCommander.newBuilder().addObject(taskOperations).build().parse(args);
  320. System.out.println("Producer = " + taskOperations.producerCount + " Consumer = " + taskOperations.consumerCount);
  321. simulateWork(taskOperations.producerCount, taskOperations.consumerCount);
  322. }
  323.  
  324. public static void simulateWork(int producerCount, int consumerCount)
  325. {
  326. BlockingQueue<Task> queue = new ArrayBlockingQueue<>(NUMBER_ELEMENT_QUEUE);
  327. TaskProducer[] producers = new TaskProducer[producerCount];
  328. for (int i = 0; i < producerCount; i++) {
  329. producers[i] = new TaskProducer(queue, NUMBER_STEPS_IN_TASK);
  330. producers[i].start();
  331. }
  332. countDownLatch = new CountDownLatch(NUMBER_STEPS_IN_TASK * producerCount);
  333. TaskExecutor[] executors = new TaskExecutor[consumerCount];
  334. for (int i = 0; i < consumerCount; i++) {
  335. executors[i] = new TaskExecutor(queue, countDownLatch);
  336. executors[i].start();
  337. }
  338. try {
  339. for (TaskProducer item : producers) {
  340. item.join();
  341. }
  342. } catch (InterruptedException e) {
  343. }
  344. try {
  345. countDownLatch.await();
  346. } catch (InterruptedException e) {
  347. }
  348. for (int i = 0; i < consumerCount; i++) {
  349. List<Executable> list = new ArrayList<>();
  350. list.add(new Executable()
  351. {
  352. @Override
  353. public boolean isEnd()
  354. {
  355. return true;
  356. }
  357.  
  358. @Override
  359. public void execute()
  360. {
  361. }
  362. });
  363. try {
  364. queue.put(new Task("End", list));
  365. } catch (InterruptedException e) {
  366. }
  367. }
  368. for (TaskExecutor item : executors) {
  369. try {
  370. item.join();
  371. } catch (InterruptedException e) {
  372. }
  373. }
  374. }
  375. }
  376.  
  377.  
  378. package ru.omsu.imit.emomonov.threads.multitask;
  379.  
  380. import ru.omsu.imit.emomonov.threads.task.Executable;
  381.  
  382. import java.util.List;
  383.  
  384. public class Task
  385. {
  386. private String name;
  387. private List<Executable> list;
  388. private int currentTaskNumber;
  389.  
  390. public Task(String name, List<Executable> list)
  391. {
  392. this.name = name;
  393. this.list = list;
  394. this.currentTaskNumber = 0;
  395. }
  396.  
  397. public String getName()
  398. {
  399. return name;
  400. }
  401.  
  402. public Executable nextTask()
  403. {
  404. return list.get(currentTaskNumber++);
  405. }
  406.  
  407. public int getListSize()
  408. {
  409. return list.size();
  410. }
  411.  
  412. public boolean isLast(){
  413. return currentTaskNumber >= list.size();
  414. }
  415. }
  416.  
  417. package ru.omsu.imit.emomonov.threads.multitask;
  418.  
  419. import ru.omsu.imit.emomonov.threads.task.Executable;
  420.  
  421. import java.util.concurrent.BlockingQueue;
  422. import java.util.concurrent.CountDownLatch;
  423.  
  424. public class TaskExecutor extends Thread
  425. {
  426. private BlockingQueue<Task> queue;
  427. private CountDownLatch countDownLatch;
  428.  
  429. public TaskExecutor(BlockingQueue<Task> queue, CountDownLatch countDownLatch)
  430. {
  431. this.queue = queue;
  432. this.countDownLatch = countDownLatch;
  433. }
  434. @Override
  435. public void run(){
  436. while (true){
  437. try {
  438. Task task = queue.take();
  439. System.out.println(task.getName());
  440. Executable executable = null;
  441. if(!task.isLast()) {
  442. executable = task.nextTask();
  443. if (executable.isEnd()) {
  444. return;
  445. } else {
  446. executable.execute();
  447. if (!task.isLast()) {
  448. queue.put(task);
  449. } else {
  450. countDownLatch.countDown();
  451. }
  452. }
  453. }
  454. } catch (InterruptedException e) {
  455. }
  456. }
  457. }
  458. }
  459.  
  460.  
  461. package ru.omsu.imit.emomonov.threads.multitask;
  462.  
  463. import ru.omsu.imit.emomonov.threads.task.Executable;
  464.  
  465. import java.util.concurrent.BlockingQueue;
  466. import java.util.concurrent.CountDownLatch;
  467.  
  468. public class TaskExecutor extends Thread
  469. {
  470. private BlockingQueue<Task> queue;
  471. private CountDownLatch countDownLatch;
  472.  
  473. public TaskExecutor(BlockingQueue<Task> queue, CountDownLatch countDownLatch)
  474. {
  475. this.queue = queue;
  476. this.countDownLatch = countDownLatch;
  477. }
  478. @Override
  479. public void run(){
  480. while (true){
  481. try {
  482. Task task = queue.take();
  483. System.out.println(task.getName());
  484. Executable executable = null;
  485. if(!task.isLast()) {
  486. executable = task.nextTask();
  487. if (executable.isEnd()) {
  488. return;
  489. } else {
  490. executable.execute();
  491. if (!task.isLast()) {
  492. queue.put(task);
  493. } else {
  494. countDownLatch.countDown();
  495. }
  496. }
  497. }
  498. } catch (InterruptedException e) {
  499. }
  500. }
  501. }
  502. }
  503. package ru.omsu.imit.emomonov.threads.multitask;
  504.  
  505. import ru.omsu.imit.emomonov.threads.task.Executable;
  506.  
  507. import java.util.concurrent.BlockingQueue;
  508. import java.util.concurrent.CountDownLatch;
  509.  
  510. public class TaskExecutor extends Thread
  511. {
  512. private BlockingQueue<Task> queue;
  513. private CountDownLatch countDownLatch;
  514.  
  515. public TaskExecutor(BlockingQueue<Task> queue, CountDownLatch countDownLatch)
  516. {
  517. this.queue = queue;
  518. this.countDownLatch = countDownLatch;
  519. }
  520. @Override
  521. public void run(){
  522. while (true){
  523. try {
  524. Task task = queue.take();
  525. System.out.println(task.getName());
  526. Executable executable = null;
  527. if(!task.isLast()) {
  528. executable = task.nextTask();
  529. if (executable.isEnd()) {
  530. return;
  531. } else {
  532. executable.execute();
  533. if (!task.isLast()) {
  534. queue.put(task);
  535. } else {
  536. countDownLatch.countDown();
  537. }
  538. }
  539. }
  540. } catch (InterruptedException e) {
  541. }
  542. }
  543. }
  544. }
  545. package ru.omsu.imit.emomonov.threads.multitask;
  546.  
  547. import ru.omsu.imit.emomonov.threads.task.Executable;
  548.  
  549. import java.util.concurrent.BlockingQueue;
  550. import java.util.concurrent.CountDownLatch;
  551.  
  552. public class TaskExecutor extends Thread
  553. {
  554. private BlockingQueue<Task> queue;
  555. private CountDownLatch countDownLatch;
  556.  
  557. public TaskExecutor(BlockingQueue<Task> queue, CountDownLatch countDownLatch)
  558. {
  559. this.queue = queue;
  560. this.countDownLatch = countDownLatch;
  561. }
  562. @Override
  563. public void run(){
  564. while (true){
  565. try {
  566. Task task = queue.take();
  567. System.out.println(task.getName());
  568. Executable executable = null;
  569. if(!task.isLast()) {
  570. executable = task.nextTask();
  571. if (executable.isEnd()) {
  572. return;
  573. } else {
  574. executable.execute();
  575. if (!task.isLast()) {
  576. queue.put(task);
  577. } else {
  578. countDownLatch.countDown();
  579. }
  580. }
  581. }
  582. } catch (InterruptedException e) {
  583. }
  584. }
  585. }
  586. }
  587. package ru.omsu.imit.emomonov.threads.multitask;
  588.  
  589. import ru.omsu.imit.emomonov.threads.task.Executable;
  590.  
  591. import java.util.concurrent.BlockingQueue;
  592. import java.util.concurrent.CountDownLatch;
  593.  
  594. public class TaskExecutor extends Thread
  595. {
  596. private BlockingQueue<Task> queue;
  597. private CountDownLatch countDownLatch;
  598.  
  599. public TaskExecutor(BlockingQueue<Task> queue, CountDownLatch countDownLatch)
  600. {
  601. this.queue = queue;
  602. this.countDownLatch = countDownLatch;
  603. }
  604. @Override
  605. public void run(){
  606. while (true){
  607. try {
  608. Task task = queue.take();
  609. System.out.println(task.getName());
  610. Executable executable = null;
  611. if(!task.isLast()) {
  612. executable = task.nextTask();
  613. if (executable.isEnd()) {
  614. return;
  615. } else {
  616. executable.execute();
  617. if (!task.isLast()) {
  618. queue.put(task);
  619. } else {
  620. countDownLatch.countDown();
  621. }
  622. }
  623. }
  624. } catch (InterruptedException e) {
  625. }
  626. }
  627. }
  628. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement