Advertisement
Guest User

Untitled

a guest
Nov 6th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.56 KB | None | 0 0
  1. public class Zadatak2 {//Metod chT() menja vrednost promenljive boolean koja se nalazi u klasi NitIspis
  2.     public static void main(String[] args) {
  3.         NitIspis ni = new NitIspis(10);
  4.         ni.setDaemon(true);
  5.         ni.start();
  6.     }
  7. }
  8.  
  9. class NitFibonaci implements Runnable {
  10.     public static int LAST;
  11.     int counter;
  12.     NitIspis pThread;
  13.    
  14.     NitFibonaci(int counter, NitIspis pThread) {
  15.         this.counter = counter;
  16.         this.pThread = pThread;
  17.     }
  18.     public void run() {
  19.         int f1 = 0;
  20.         int f2 = 1;
  21.         while (counter > 0) {
  22.             if (!pThread.turn) {
  23.                 int t = f1+f2;
  24.                 f1 = f2;
  25.                 f2 = t;
  26.                 LAST = f1;
  27.                 counter--;
  28.                 pThread.chT();
  29.             }
  30.         }
  31.         pThread.interrupt();
  32.     }
  33. }
  34.  
  35. class NitIspis extends Thread {
  36.     int counter;
  37.     boolean turn;
  38.    
  39.     NitIspis(int counter) {
  40.         this.counter = counter;
  41.         this.turn = true;
  42.     }
  43.     public void run() {
  44.         NitFibonaci r = new NitFibonaci(counter, this);
  45.         Thread nf = new Thread(r);
  46.         nf.start();
  47.        
  48.         while (true) {
  49.             if (turn) {
  50.                 System.out.println(""+r.LAST);
  51.                 turn = !turn;
  52.             } else {
  53.                 continue;
  54.             }
  55.             try {
  56.                 Thread.sleep(1);
  57.             } catch (InterruptedException e) {
  58.                 return;
  59.             }
  60.         }
  61.     }
  62.     public void chT() {
  63.         turn = !turn;
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement