Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import java.time.LocalDate;
  2. import java.time.format.DateTimeFormatter;
  3. import java.text.SimpleDateFormat;
  4. import java.util.*;
  5.  
  6. public class ThreadTest {
  7.  
  8. /*skapa instans av Television*/
  9. TelevisionStation tv;
  10.  
  11. public ThreadTest() {
  12. tv = new TelevisionStation();
  13.  
  14. /*skapa instans av 2 tråder */
  15. Thread th1 = new MyThread1();
  16. Thread th2 = new MyThread2();
  17. th1.start();
  18. th2.start();
  19.  
  20. try {
  21. Thread.sleep(12000);
  22. } catch (InterruptedException e) {
  23. e.printStackTrace();
  24. }
  25. Thread th3 = new MyThread1();
  26. th3.start();
  27. }
  28.  
  29. public static void main(String[] args) {
  30. new ThreadTest();
  31.  
  32. }
  33.  
  34. /*skapa två olika tråd som ska köra instans av TelevisionStation*/
  35. /*tråd 1: instan av TelevisionStation anropa metoder teleprompter*/
  36. class MyThread1 extends Thread {
  37. @Override
  38. public void run() {
  39. tv.showTeleprompter();
  40. System.out.println("Första tråd kör färdig");
  41. }
  42. }
  43.  
  44. /*tråden skapar parametra for metoder addHeadline med hjälp av rand funktion*/
  45. class MyThread2 extends Thread {
  46. @SuppressWarnings("deprecation")
  47. @Override
  48. public void run() {
  49. Random rand = new Random();
  50. for (int i = 0; i < 200; i++) {
  51. tv.addHeadline(new Date(2014, rand.nextInt(12), rand.nextInt(27)), "abcdefgh" + i);
  52. }
  53. System.out.println("Andra Tråd kör färdig ");
  54. }
  55. }
  56. }
  57.  
  58. class TelevisionStation extends MediaCompany {
  59. private final static SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
  60.  
  61. /* Väljer att ändra från HashMap till LinkedHashMap för att få till ordningen på värden som läggs in. */
  62. public Map<String, Collection<String>> headlines = new LinkedHashMap<>();
  63.  
  64. public TelevisionStation() {
  65. Timer timer = new Timer();
  66. timer.schedule(new TimerTask() {
  67. public void run() {
  68. if (headlines.size() > 100) {
  69. System.out.println("Size of headlines too large: " +
  70. headlines.size());
  71. }
  72. }
  73. }, 10_000, 10_000);
  74. }
  75.  
  76. /* Här smiter referensen headlines till en abstrakt metod.*/
  77. public void showTeleprompter() {
  78. printHeadlines(headlines.values());
  79. }
  80.  
  81. public void addHeadline(Date date, String line) {
  82. String day = dayFormat.format(date);
  83. Collection<String> lines = headlines.get(day);
  84. if (lines == null) {
  85. lines = new ArrayList<String>();
  86. headlines.put(day, lines);
  87. }
  88. lines.add(line);
  89. }
  90. }
  91.  
  92. abstract class MediaCompany {
  93.  
  94. public void printHeadlines(Collection<Collection<String>> headlines) {
  95. for (Collection<String> headline : headlines) {
  96. for (String line : headline) {
  97. System.out.println(line);
  98. }
  99. }
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement