Advertisement
Guest User

Zadatak1

a guest
Nov 3rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.23 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4.  /*Please dont change class name, Dcoder
  5.  and class must not be public*/
  6.  
  7.  //Compiler version JDK 1.8
  8.  
  9.  
  10.  class Dcoder {
  11.     public static void main(String args[]) {
  12.        
  13.         Suma s = new Suma();
  14.         Thread sume = new Thread(s);
  15.         Ispis ispis = new Ispis(s);
  16.         int p = 10;//mrzi me da kucam ono sa tastature
  17.         sume.start();
  18.         ispis.start();
  19.         while(p!=0) {
  20.             p--;
  21.             try {
  22.                 Thread.sleep(1000);
  23.             } catch (InterruptedException e) {
  24.                 return;
  25.             }
  26.         }
  27.         ispis.interrupt();
  28.         sume.interrupt();
  29.         try {
  30.             ispis.join();
  31.             sume.join();
  32.         } catch (InterruptedException e) {
  33.             e.printStackTrace();
  34.         }
  35.     }
  36.  }
  37.  
  38. class Suma implements Runnable {
  39.     public static volatile int last;
  40.    
  41.     public Suma() {
  42.         this.last = 0;
  43.     }
  44.     public void run() {
  45.         int i = 0;
  46.         while (true) {
  47.             i += i + 1;
  48.             try {
  49.                 Thread.sleep(100);
  50.             } catch (InterruptedException e) {
  51.                 return;
  52.             }
  53.             last = i;
  54.         }
  55.     }
  56. }
  57.  
  58. class Ispis extends Thread {
  59.     Suma s;
  60.    
  61.     public Ispis (Suma s) {
  62.         this.s = s;
  63.     }
  64.    
  65.     public void run() {
  66.         while(true) {
  67.             System.out.println(""+s.last);
  68.             try {
  69.                 Thread.sleep(100);
  70.             } catch (InterruptedException e) {
  71.                 return;
  72.             }
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement