Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Svaka od niti nezavisno izracunava i ispisuje prvih 5 elemenata svog niza prirodnih brojeva.
- * Za to vreme glavna nit ucitava red po red sa tastature i zavrsava rad celog programa kada
- * se ucita "Kraj".
- *
- * nitF --> fibonaci
- * nitK --> K_0 = 1, K_n = n^2;
- **/
- //ISPRAVITI POSTOJECU/E GRESKU/E
- import java.io.*;
- public class Vezba1 extends Thread
- {
- //number of iterations
- private final int n = 50;
- //element n && element n+1
- private int temp, next;
- //Thread name
- private String name;
- public Vezba1(int temp, int next, String name){
- setName("nit" + name);
- this.name = name;
- setDaemon(true);
- this.temp = temp;
- this.next = next;
- }
- public void run() {
- System.out.println(getName() + " " + temp);
- System.out.println(getName() + " " + next);
- for(int i = 0; i < 2; i++){
- if(isInterrupted())
- break;
- int t = izracunaj(temp, next, i);
- temp = next;
- next = t;
- System.out.println(getName() + ", medju rezultat: " + t);
- }
- }
- private int izracunaj(int temp, int next, int t){
- if(name.equals("f")){
- return temp + next;
- } else if(name.equals("k")){
- return (int)(Math.pow(t, 2));
- } else {
- System.out.println("Greska: ime nije validno");
- return 0;
- }
- }
- public static void main(String[] args){
- Thread nitF = new Vezba1(0, 1, "f");
- Thread nitK = new Vezba1(0, (int)(Math.pow(1, 2)), "k");
- nitF.start();
- nitK.start();
- BufferedReader reader = null;
- try{
- reader = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- while(line.equals("Kraj")){
- line = reader.readLine();
- }
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- try{
- reader.close();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement