Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main{
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- potok R = new potok();
- int menu;
- do{
- System.out.println("Enter menu");
- System.out.println("1. Start");
- System.out.println("2. Continue");
- System.out.println("3. Pause");
- System.out.println("4. Print");
- System.out.println("5. Print time");
- System.out.println("0. Exit");
- menu = in.nextInt();
- switch(menu){
- case 1:
- R.start();
- break;
- case 2:
- synchronized(R){
- R.notify();
- }
- R.FlPause = false;
- break;
- case 3:
- R.FlPause = true;
- break;
- case 4:
- System.out.println(R.getPi());
- break;
- case 5:
- R.getTime();
- break;
- }
- }while(menu != 0);
- }
- }
- import java.lang.Math;
- public class potok extends Thread {
- public volatile boolean FlPause = false;
- public boolean FlCoun = true;
- public double pi = 0;
- volatile long timeStart;
- volatile long timeSpend;
- volatile long time;
- @Override
- public void run() {
- long i = 0;
- timeStart = System.currentTimeMillis();
- while (FlCoun) {
- pi = pi + (Math.pow(-1, i) / (2 * i + 1));
- i++;
- if (FlPause) {
- timeSpend = System.currentTimeMillis() - timeStart;
- time += timeSpend;
- try {
- synchronized (this){
- wait();
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public double getPi() {
- return (4 * pi);
- }
- public void getTime() {
- System.out.println("Seconds: " + time/1000);
- time = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement