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;
- boolean exitFlag = false;
- boolean interrupt = true;
- int i = 1;
- 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();
- FThread fThread = new FThread(x , ex1);
- GThread gThread = new GThread(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){}
- if(!sms1.equals("I am alive!")){
- exitFlag = true;
- try{
- sms1 = ex1.exchange("тобi пiзда",50,TimeUnit.MILLISECONDS);
- }catch (TimeoutException ex){}
- gThread.interrupt();
- break;
- }
- try{
- sms2 = ex2.exchange("123",50,TimeUnit.MILLISECONDS);
- }catch (TimeoutException ex){}
- if(!sms2.equals("I am alive!")){
- exitFlag = true;
- try{
- sms2 = ex2.exchange("тобi пiзда",50,TimeUnit.MILLISECONDS);
- }catch (TimeoutException ex){}
- break;
- }
- if(fThread.done == true && fThread.result == false){
- exitFlag = 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(gThread.done == true && gThread.result == false){
- exitFlag = 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, yy - continue and never ask again, other - terminate\n");
- String str = keyboard.next();
- switch (str) {
- case "y":
- break;
- case "yy":
- interrupt = false;
- break;
- default:
- exitFlag = true;
- //fThread.interrupt();
- 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));//fThread.result * gThread.result != 0
- if(!exitFlag)
- System.out.println("f(x)&&g(x) = " + (fThread.result && gThread.result));
- }
- }
- class FThread extends Thread {
- private int x;
- public volatile boolean result = true;
- public volatile boolean done = false;
- Exchanger<String> exchanger;
- String sms;
- FThread(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());
- }
- }
- private boolean f(int x) throws InterruptedException {
- for(int i=0; i<500; i++) {
- if(interrupted())
- throw new InterruptedException();
- sleep(50);
- Read();
- }
- done = true;
- return x > 10;
- }
- @Override
- public void run(){
- try {
- result = f(x);
- } catch(InterruptedException ex){
- return;
- }
- }
- }
- class GThread extends Thread {
- private int x;
- public volatile boolean result = true;
- public volatile boolean done = false;
- Exchanger<String> exchanger;
- String sms;
- Random rnd = new Random();
- GThread(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());
- }
- }
- private boolean g(int x) throws InterruptedException {
- /* for(int i=0; i<505; i++) {
- if(interrupted())
- throw new InterruptedException();
- sleep(50);
- Read();
- }*/
- for(int i = 0 ; i < 50 ; ++i){
- Read();
- if(interrupted())
- throw new InterruptedException();
- if((rnd.nextInt() % 100) == x){
- done = true;
- return true;
- }
- }
- done = true;
- return false;
- // return x > 20;
- }
- @Override
- public void run(){
- try {
- result = g(x);
- } catch(InterruptedException ex){
- return;
- }
- }
- }
- class funcThread extends Thread {
- private int x;
- public volatile boolean result = true;
- public volatile boolean done = false;
- Exchanger<String> exchanger;
- String sms;
- Random rnd = new Random();
- 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;
- // return x > 20;
- }
- @Override
- public void run(){
- try {
- result = g(x);
- } catch(InterruptedException ex){
- return;
- }
- }
- }
- class F extends funcThread{
- // private int x;
- // public volatile boolean result = true;
- // public volatile boolean done = false;
- // Exchanger<String> exchanger;
- // String sms;
- F(int _x , Exchanger ex){
- super(_x , ex);
- // x = _x;
- // exchanger = ex;
- }
- @Override
- public boolean g(int x) throws InterruptedException{
- for(int i = 0 ; i < 50 ; ++i){
- Read();
- if(interrupted())
- throw new InterruptedException();
- if((rnd.nextInt() % 100) == x){
- done = true;
- return true;
- }
- }
- done = true;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement