Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ZADACA 1
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class TwoThreads {
- public static class Thread1 extends Thread {
- public void run() {
- System.out.println("A");
- System.out.println("B");
- }
- }
- public static class Thread2 extends Thread {
- public void run() {
- System.out.println("1");
- System.out.println("2");
- }
- }
- public static void main(String[] args) {
- //new Thread1().start();
- //new Thread2().start();
- new ThreadAB("A\nB").run();
- new ThreadAB("1\n2").run();
- }
- }
- class ThreadAB{
- private String data;
- public ThreadAB(String string){
- data = string;
- }
- public void run(){
- System.out.println(data);
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ZADACA 2
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- import java.util.Arrays;
- import java.util.Random;
- public class TenThreads {
- private static class WorkerThread implements Runnable {
- int max = Integer.MIN_VALUE;
- int[] ourArray;
- public WorkerThread(int[] ourArray) {
- this.ourArray = ourArray;
- }
- // Find the maximum value in our particular piece of the array
- public void run() {
- for (int i = 0; i < ourArray.length; i++)
- max = Math.max(max, ourArray[i]);
- }
- public int getMax() {
- return max;
- }
- }
- public static void main(String[] args) {
- Runnable[] runnables = new WorkerThread[20];
- int[][] bigMatrix = getBigHairyMatrix();
- int max = Integer.MIN_VALUE;
- // Give each thread a slice of the matrix to work with
- for (int i = 0; i < 20; i += 2) {
- runnables[i] = new WorkerThread(Arrays.copyOfRange(bigMatrix[i],0, 49));
- runnables[i + 1] = new WorkerThread(Arrays.copyOfRange(bigMatrix[i],50, 99));
- }
- Thread[] threads = new Thread[20];
- for (int i = 0; i < 20; i++){
- threads[i] = new Thread(runnables[i]);
- }
- for (int i = 0; i < 20; i++){
- threads[i].start();
- }
- // Wait for each thread to finish
- try {
- for (int i = 0; i < 10; i++) {
- threads[i].join(); // why is this needed
- max = Math.max(max, ((WorkerThread)(runnables[i])).getMax());
- }
- } catch (InterruptedException e) {
- // fall through
- }
- System.out.println("Maximum value was " + max);
- }
- static int[][] getBigHairyMatrix() {
- int x = 100;
- int y = 100;
- int[][] matrix = new int[x][y];
- Random rnd = new Random();
- for (int i = 0; i < x; i++)
- for (int j = 0; j < y; j++) {
- matrix[i][j] = rnd.nextInt();
- }
- return matrix;
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ZADACA 4
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- import java.util.HashSet;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.concurrent.Semaphore;
- public class CountThree {
- public static int NUM_RUNS = 10;
- /**
- * Promenlivata koja treba da go sodrzi brojot na pojavuvanja na elementot 3
- */
- int count = 0;
- private Semaphore countSemaphore;
- /**
- * TODO: definirajte gi potrebnite elementi za sinhronizacija
- */
- public void init() {
- countSemaphore = new Semaphore(1);
- }
- class Counter extends Thread {
- public void count(int[] data) throws InterruptedException {
- // da se implementira
- int currentCount = 0;
- for (int number : data){
- if (number == 3)
- currentCount++;
- }
- countSemaphore.acquire();
- count += currentCount;
- countSemaphore.release();
- }
- private int[] data;
- public Counter(int[] data) {
- this.data = data;
- }
- @Override
- public void run() {
- try {
- count(data);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- try {
- CountThree environment = new CountThree();
- environment.start();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public void start() throws Exception {
- init();
- HashSet<Thread> threads = new HashSet<Thread>();
- Scanner s = new Scanner(System.in);
- int total=s.nextInt();
- for (int i = 0; i < NUM_RUNS; i++) {
- int[] data = new int[total];
- for (int j = 0; j < total; j++) {
- data[j] = s.nextInt();
- }
- Counter c = new Counter(data);
- threads.add(c);
- }
- for (Thread t : threads) {
- t.start();
- }
- for (Thread t : threads) {
- t.join();
- }
- System.out.println(count);
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement