Advertisement
lashrone1

potok

Sep 19th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class Main{
  5.     public static void main(String[] args){
  6.         Scanner in = new Scanner(System.in);
  7.         potok R = new potok();
  8.         int menu;
  9.         do{
  10.             System.out.println("Enter menu");
  11.             System.out.println("1. Start");
  12.             System.out.println("2. Continue");
  13.             System.out.println("3. Pause");
  14.             System.out.println("4. Print");
  15.             System.out.println("5. Print time");
  16.             System.out.println("0. Exit");
  17.             menu = in.nextInt();
  18.             switch(menu){
  19.                 case 1:
  20.                     R.start();
  21.                     break;
  22.                 case 2:
  23.                     synchronized(R){
  24.                         R.notify();
  25.                     }
  26.                     R.FlPause = false;
  27.                     break;
  28.                 case 3:
  29.                     R.FlPause = true;
  30.                     break;
  31.                 case 4:
  32.                     System.out.println(R.getPi());
  33.                     break;
  34.                 case 5:
  35.                     R.getTime();
  36.                     break;
  37.             }
  38.         }while(menu != 0);
  39.     }
  40. }
  41. import java.lang.Math;
  42.  
  43. public class potok extends Thread {
  44.     public volatile boolean FlPause = false;
  45.     public boolean FlCoun = true;
  46.     public double pi = 0;
  47.     volatile long timeStart;
  48.     volatile long timeSpend;
  49.     volatile long time;
  50.  
  51.     @Override
  52.     public void run() {
  53.         long i = 0;
  54.         timeStart = System.currentTimeMillis();
  55.  
  56.         while (FlCoun) {
  57.             pi = pi + (Math.pow(-1, i) / (2 * i + 1));
  58.             i++;
  59.  
  60.             if (FlPause) {
  61.                 timeSpend = System.currentTimeMillis() - timeStart;
  62.                 time += timeSpend;
  63.                 try {
  64.                     synchronized (this){
  65.                         wait();
  66.                     }
  67.                 } catch (InterruptedException e) {
  68.                     e.printStackTrace();
  69.                 }
  70.             }
  71.         }
  72.  
  73.     }
  74.  
  75.  
  76.     public double getPi() {
  77.         return (4 * pi);
  78.     }
  79.  
  80.     public void getTime() {
  81.         System.out.println("Seconds: " + time/1000);
  82.         time = 0;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement