Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Zadatak2 {//Metod chT() menja vrednost promenljive boolean koja se nalazi u klasi NitIspis
- public static void main(String[] args) {
- NitIspis ni = new NitIspis(10);
- ni.setDaemon(true);
- ni.start();
- }
- }
- class NitFibonaci implements Runnable {
- public static int LAST;
- int counter;
- NitIspis pThread;
- NitFibonaci(int counter, NitIspis pThread) {
- this.counter = counter;
- this.pThread = pThread;
- }
- public void run() {
- int f1 = 0;
- int f2 = 1;
- while (counter > 0) {
- if (!pThread.turn) {
- int t = f1+f2;
- f1 = f2;
- f2 = t;
- LAST = f1;
- counter--;
- pThread.chT();
- }
- }
- pThread.interrupt();
- }
- }
- class NitIspis extends Thread {
- int counter;
- boolean turn;
- NitIspis(int counter) {
- this.counter = counter;
- this.turn = true;
- }
- public void run() {
- NitFibonaci r = new NitFibonaci(counter, this);
- Thread nf = new Thread(r);
- nf.start();
- while (true) {
- if (turn) {
- System.out.println(""+r.LAST);
- turn = !turn;
- } else {
- continue;
- }
- try {
- Thread.sleep(1);
- } catch (InterruptedException e) {
- return;
- }
- }
- }
- public void chT() {
- turn = !turn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement