Advertisement
osipyonok

Lab4 SP

Dec 8th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.util.Random;
  5. import java.util.Scanner;
  6. import java.util.concurrent.Exchanger;
  7. import java.util.concurrent.TimeUnit;
  8. import java.util.concurrent.TimeoutException;
  9. import static java.lang.Thread.sleep;
  10.  
  11. public class Main {
  12.  
  13.     public static final int SECONDS_DELAY = 10;
  14.  
  15.     public static void main(String[] args) {
  16.         Scanner keyboard = new Scanner(System.in);
  17.         int x , i = 1;
  18.         boolean exitFlag = false;
  19.         boolean interrupt = true;
  20.         Exchanger<String> ex1 = new Exchanger();
  21.         Exchanger<String> ex2 = new Exchanger();
  22.         String sms1 = "I am alive!";
  23.         String sms2 = "I am alive!";
  24.  
  25.         System.out.printf("Enter x: ");
  26.         x = keyboard.nextInt();
  27.  
  28.  
  29.         F fThread = new F(x , ex1);
  30.         G gThread = new G(x , ex2);
  31.         fThread.start();
  32.         gThread.start();
  33.  
  34.         do try {
  35.             sleep(50);
  36.             System.out.println(i);
  37.  
  38.             try{
  39.                 sms1 = ex1.exchange("123",50,TimeUnit.MILLISECONDS);
  40.             }catch (TimeoutException ex){}
  41.             try{
  42.                 sms2 = ex2.exchange("123",50,TimeUnit.MILLISECONDS);
  43.             }catch (TimeoutException ex){}
  44.             if(((!sms1.equals("I am alive!")) || (!sms2.equals("I am alive!")))
  45.                     || ((fThread.done == true && fThread.result == false) || (gThread.done == true && gThread.result == false))) {
  46.                 exitFlag = (fThread.done == true && fThread.result == false) || (gThread.done == true && gThread.result == false);
  47.                 try {
  48.                     sms1 = ex1.exchange("тобi пiзда", 50, TimeUnit.MILLISECONDS);
  49.                 } catch (TimeoutException ex) {}
  50.                 try {
  51.                     sms2 = ex2.exchange("тобi пiзда", 50, TimeUnit.MILLISECONDS);
  52.                 } catch (TimeoutException ex) {}
  53.                 break;
  54.             }
  55.             if (i % (SECONDS_DELAY * 1000 / 50) == 0 && interrupt) { // 10 seconds
  56.                 System.out.printf("Calculations take longer than expected. Do you want to continue?\ny - yes, yes - continue and never ask again, else - terminate\n");
  57.                 String str = keyboard.next();
  58.                 switch (str) {
  59.                     case "y":
  60.                         break;
  61.                     case "yes":
  62.                         interrupt = false;
  63.                         break;
  64.                     default:
  65.                         exitFlag = true;
  66.                         try {
  67.                             sms1 = ex1.exchange("тобi пiзда", 50, TimeUnit.MILLISECONDS);
  68.                         } catch (TimeoutException ex) {}
  69.                         try {
  70.                             sms2 = ex2.exchange("тобi пiзда", 50, TimeUnit.MILLISECONDS);
  71.                         } catch (TimeoutException ex) {}
  72.                         break;
  73.                 }
  74.             }
  75.             i = ++i % (SECONDS_DELAY * 1000 / 50);
  76.         } catch (InterruptedException e) {
  77.             e.printStackTrace();
  78.         }
  79.         while (((fThread.isAlive() || gThread.isAlive()) && !exitFlag) && !(fThread.done && gThread.done));
  80.         if (!exitFlag)
  81.             System.out.println("f(x)&&g(x) = " + (fThread.result && gThread.result));
  82.     }
  83. }
  84.  
  85.  
  86. class funcThread extends Thread {
  87.     private int x;
  88.     public volatile boolean result = true;
  89.     public volatile boolean done = false;
  90.     Exchanger<String> exchanger;
  91.     String sms;
  92.  
  93.  
  94.     funcThread(int _x , Exchanger ex){
  95.         super();
  96.  
  97.         exchanger = ex;
  98.         x = _x;
  99.     }
  100.  
  101.     public void Read(){
  102.         try{
  103.             String mess = this.isAlive() ? "I am alive!" : "зараз як уїбу сука";
  104.             sms = exchanger.exchange(mess);
  105.             if(sms.equals("тобi пiзда"))this.interrupt();
  106.         }catch (InterruptedException ex){
  107.             System.out.println(ex.getMessage());
  108.         }
  109.     }
  110.  
  111.     public boolean g(int x) throws InterruptedException {
  112.         return false;
  113.     }
  114.  
  115.     @Override
  116.     public void run(){
  117.         try {
  118.             result = g(x);
  119.         } catch(InterruptedException ex){
  120.             return;
  121.         }
  122.     }
  123. }
  124.  
  125. class F extends  funcThread{
  126.     Random rnd = new Random();
  127.    
  128.     F(int _x , Exchanger ex){
  129.         super(_x , ex);
  130.     }
  131.  
  132.     @Override
  133.     public boolean g(int x) throws InterruptedException{
  134.         for(int i = 0 ; i < 150 ; ++i){
  135.             Read();
  136.             if(interrupted())
  137.                 throw new InterruptedException();
  138.             if((rnd.nextInt() % 30) == x){
  139.                 done = true;
  140.                 return true;
  141.             }
  142.         }
  143.  
  144.         done = true;
  145.         return false;
  146.     }
  147. }
  148.  
  149. class G extends funcThread{
  150.     G(int _x , Exchanger ex){
  151.         super(_x , ex);
  152.     }
  153.  
  154.     @Override
  155.     public boolean g(int x) throws InterruptedException{
  156.         for(int i = 0 ; i < 500 ; ++i){
  157.             if(interrupted())
  158.                 throw new InterruptedException();
  159.             sleep(50);
  160.             Read();
  161.         }
  162.         done = true;
  163.         return x > 10;
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement