Advertisement
NHristovski

Untitled

Mar 11th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. ZADACA 1
  2. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3. public class TwoThreads {
  4. public static class Thread1 extends Thread {
  5. public void run() {
  6. System.out.println("A");
  7. System.out.println("B");
  8.  
  9. }
  10. }
  11.  
  12. public static class Thread2 extends Thread {
  13. public void run() {
  14. System.out.println("1");
  15. System.out.println("2");
  16.  
  17. }
  18. }
  19.  
  20. public static void main(String[] args) {
  21. //new Thread1().start();
  22. //new Thread2().start();
  23. new ThreadAB("A\nB").run();
  24. new ThreadAB("1\n2").run();
  25. }
  26.  
  27. }
  28. class ThreadAB{
  29. private String data;
  30. public ThreadAB(String string){
  31. data = string;
  32. }
  33.  
  34. public void run(){
  35. System.out.println(data);
  36. }
  37. }
  38. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  39.  
  40. ZADACA 2
  41. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. import java.util.Arrays;
  43. import java.util.Random;
  44.  
  45. public class TenThreads {
  46. private static class WorkerThread implements Runnable {
  47. int max = Integer.MIN_VALUE;
  48. int[] ourArray;
  49.  
  50. public WorkerThread(int[] ourArray) {
  51. this.ourArray = ourArray;
  52. }
  53.  
  54. // Find the maximum value in our particular piece of the array
  55. public void run() {
  56. for (int i = 0; i < ourArray.length; i++)
  57. max = Math.max(max, ourArray[i]);
  58. }
  59.  
  60. public int getMax() {
  61. return max;
  62. }
  63. }
  64.  
  65. public static void main(String[] args) {
  66. Runnable[] runnables = new WorkerThread[20];
  67.  
  68. int[][] bigMatrix = getBigHairyMatrix();
  69. int max = Integer.MIN_VALUE;
  70.  
  71. // Give each thread a slice of the matrix to work with
  72. for (int i = 0; i < 20; i += 2) {
  73. runnables[i] = new WorkerThread(Arrays.copyOfRange(bigMatrix[i],0, 49));
  74. runnables[i + 1] = new WorkerThread(Arrays.copyOfRange(bigMatrix[i],50, 99));
  75. }
  76. Thread[] threads = new Thread[20];
  77. for (int i = 0; i < 20; i++){
  78. threads[i] = new Thread(runnables[i]);
  79. }
  80. for (int i = 0; i < 20; i++){
  81. threads[i].start();
  82. }
  83. // Wait for each thread to finish
  84. try {
  85. for (int i = 0; i < 10; i++) {
  86. threads[i].join(); // why is this needed
  87. max = Math.max(max, ((WorkerThread)(runnables[i])).getMax());
  88. }
  89. } catch (InterruptedException e) {
  90. // fall through
  91. }
  92.  
  93. System.out.println("Maximum value was " + max);
  94. }
  95.  
  96. static int[][] getBigHairyMatrix() {
  97. int x = 100;
  98. int y = 100;
  99.  
  100. int[][] matrix = new int[x][y];
  101. Random rnd = new Random();
  102.  
  103. for (int i = 0; i < x; i++)
  104. for (int j = 0; j < y; j++) {
  105. matrix[i][j] = rnd.nextInt();
  106. }
  107.  
  108. return matrix;
  109. }
  110.  
  111. }
  112. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  113.  
  114. ZADACA 4
  115. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  116. import java.util.HashSet;
  117. import java.util.Random;
  118. import java.util.Scanner;
  119. import java.util.concurrent.Semaphore;
  120.  
  121. public class CountThree {
  122.  
  123. public static int NUM_RUNS = 10;
  124. /**
  125. * Promenlivata koja treba da go sodrzi brojot na pojavuvanja na elementot 3
  126. */
  127. int count = 0;
  128. private Semaphore countSemaphore;
  129. /**
  130. * TODO: definirajte gi potrebnite elementi za sinhronizacija
  131. */
  132. public void init() {
  133. countSemaphore = new Semaphore(1);
  134. }
  135.  
  136. class Counter extends Thread {
  137.  
  138. public void count(int[] data) throws InterruptedException {
  139. // da se implementira
  140. int currentCount = 0;
  141. for (int number : data){
  142. if (number == 3)
  143. currentCount++;
  144. }
  145. countSemaphore.acquire();
  146. count += currentCount;
  147. countSemaphore.release();
  148. }
  149.  
  150. private int[] data;
  151.  
  152. public Counter(int[] data) {
  153. this.data = data;
  154. }
  155.  
  156. @Override
  157. public void run() {
  158. try {
  159. count(data);
  160. } catch (Exception e) {
  161. e.printStackTrace();
  162. }
  163. }
  164. }
  165.  
  166. public static void main(String[] args) {
  167. try {
  168. CountThree environment = new CountThree();
  169. environment.start();
  170. } catch (Exception ex) {
  171. ex.printStackTrace();
  172. }
  173. }
  174.  
  175. public void start() throws Exception {
  176.  
  177. init();
  178.  
  179. HashSet<Thread> threads = new HashSet<Thread>();
  180. Scanner s = new Scanner(System.in);
  181. int total=s.nextInt();
  182.  
  183. for (int i = 0; i < NUM_RUNS; i++) {
  184. int[] data = new int[total];
  185. for (int j = 0; j < total; j++) {
  186. data[j] = s.nextInt();
  187. }
  188. Counter c = new Counter(data);
  189. threads.add(c);
  190. }
  191.  
  192. for (Thread t : threads) {
  193. t.start();
  194. }
  195.  
  196. for (Thread t : threads) {
  197. t.join();
  198. }
  199. System.out.println(count);
  200.  
  201.  
  202. }
  203. }
  204. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement