Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.concurrent.Exchanger;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.TimeoutException;
- import static java.lang.Thread.sleep;
- public class Main {
- public static final int SECONDS_DELAY = 10;
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in);
- int x , i = 1;
- boolean exitFlag = false;
- boolean interrupt = true;
- Exchanger<String> ex1 = new Exchanger();
- Exchanger<String> ex2 = new Exchanger();
- String sms1 = "I am alive!";
- String sms2 = "I am alive!";
- System.out.printf("Enter x: ");
- x = keyboard.nextInt();
- F fThread = new F(x , ex1);
- G gThread = new G(x , ex2);
- fThread.start();
- gThread.start();
- do try {
- sleep(50);
- System.out.println(i);
- try{
- sms1 = ex1.exchange("123",50,TimeUnit.MILLISECONDS);
- }catch (TimeoutException ex){}
- try{
- sms2 = ex2.exchange("123",50,TimeUnit.MILLISECONDS);
- }catch (TimeoutException ex){}
- if(((!sms1.equals("I am alive!")) || (!sms2.equals("I am alive!")))
- || ((fThread.done == true && fThread.result == false) || (gThread.done == true && gThread.result == false))) {
- exitFlag = (fThread.done == true && fThread.result == false) || (gThread.done == true && gThread.result == false);
- try {
- sms1 = ex1.exchange("тобi пiзда", 50, TimeUnit.MILLISECONDS);
- } catch (TimeoutException ex) {}
- try {
- sms2 = ex2.exchange("тобi пiзда", 50, TimeUnit.MILLISECONDS);
- } catch (TimeoutException ex) {}
- break;
- }
- if (i % (SECONDS_DELAY * 1000 / 50) == 0 && interrupt) { // 10 seconds
- System.out.printf("Calculations take longer than expected. Do you want to continue?\ny - yes, yes - continue and never ask again, else - terminate\n");
- String str = keyboard.next();
- switch (str) {
- case "y":
- break;
- case "yes":
- interrupt = false;
- break;
- default:
- exitFlag = true;
- try {
- sms1 = ex1.exchange("тобi пiзда", 50, TimeUnit.MILLISECONDS);
- } catch (TimeoutException ex) {}
- try {
- sms2 = ex2.exchange("тобi пiзда", 50, TimeUnit.MILLISECONDS);
- } catch (TimeoutException ex) {}
- break;
- }
- }
- i = ++i % (SECONDS_DELAY * 1000 / 50);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- while (((fThread.isAlive() || gThread.isAlive()) && !exitFlag) && !(fThread.done && gThread.done));
- if (!exitFlag)
- System.out.println("f(x)&&g(x) = " + (fThread.result && gThread.result));
- }
- }
- class funcThread extends Thread {
- private int x;
- public volatile boolean result = true;
- public volatile boolean done = false;
- Exchanger<String> exchanger;
- String sms;
- funcThread(int _x , Exchanger ex){
- super();
- exchanger = ex;
- x = _x;
- }
- public void Read(){
- try{
- String mess = this.isAlive() ? "I am alive!" : "зараз як уїбу сука";
- sms = exchanger.exchange(mess);
- if(sms.equals("тобi пiзда"))this.interrupt();
- }catch (InterruptedException ex){
- System.out.println(ex.getMessage());
- }
- }
- public boolean g(int x) throws InterruptedException {
- return false;
- }
- @Override
- public void run(){
- try {
- result = g(x);
- } catch(InterruptedException ex){
- return;
- }
- }
- }
- class F extends funcThread{
- Random rnd = new Random();
- F(int _x , Exchanger ex){
- super(_x , ex);
- }
- @Override
- public boolean g(int x) throws InterruptedException{
- for(int i = 0 ; i < 150 ; ++i){
- Read();
- if(interrupted())
- throw new InterruptedException();
- if((rnd.nextInt() % 30) == x){
- done = true;
- return true;
- }
- }
- done = true;
- return false;
- }
- }
- class G extends funcThread{
- G(int _x , Exchanger ex){
- super(_x , ex);
- }
- @Override
- public boolean g(int x) throws InterruptedException{
- for(int i = 0 ; i < 500 ; ++i){
- if(interrupted())
- throw new InterruptedException();
- sleep(50);
- Read();
- }
- done = true;
- return x > 10;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement