Advertisement
osipyonok

Untitled

Dec 8th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 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.  
  10. import static java.lang.Thread.sleep;
  11.  
  12. public class Main {
  13.  
  14. public static final int SECONDS_DELAY = 10;
  15.  
  16. public static void main(String[] args) {
  17. Scanner keyboard = new Scanner(System.in);
  18. int x;
  19. boolean exitFlag = false;
  20. boolean interrupt = true;
  21. int i = 1;
  22. Exchanger<String> ex1 = new Exchanger();
  23. Exchanger<String> ex2 = new Exchanger();
  24. String sms1 = "I am alive!";
  25. String sms2 = "I am alive!";
  26.  
  27. System.out.printf("Enter x: ");
  28. x = keyboard.nextInt();
  29.  
  30. FThread fThread = new FThread(x , ex1);
  31. GThread gThread = new GThread(x , ex2);
  32.  
  33. fThread.start();
  34. gThread.start();
  35.  
  36. do try {
  37. sleep(50);
  38. System.out.println(i);
  39.  
  40. try{
  41. sms1 = ex1.exchange("123",50,TimeUnit.MILLISECONDS);
  42. }catch (TimeoutException ex){}
  43. if(!sms1.equals("I am alive!")){
  44. exitFlag = true;
  45. try{
  46. sms1 = ex1.exchange("тобi пiзда",50,TimeUnit.MILLISECONDS);
  47. }catch (TimeoutException ex){}
  48. gThread.interrupt();
  49. break;
  50. }
  51. try{
  52. sms2 = ex2.exchange("123",50,TimeUnit.MILLISECONDS);
  53. }catch (TimeoutException ex){}
  54. if(!sms2.equals("I am alive!")){
  55. exitFlag = true;
  56. try{
  57. sms2 = ex2.exchange("тобi пiзда",50,TimeUnit.MILLISECONDS);
  58. }catch (TimeoutException ex){}
  59. break;
  60. }
  61. if(fThread.done == true && fThread.result == false){
  62. exitFlag = false;
  63. try{
  64. sms1 = ex1.exchange("тобi пiзда",50,TimeUnit.MILLISECONDS);
  65. }catch (TimeoutException ex){}
  66. try{
  67. sms2 = ex2.exchange("тобi пiзда",50,TimeUnit.MILLISECONDS);
  68. }catch (TimeoutException ex){}
  69. break;
  70. }
  71. if(gThread.done == true && gThread.result == false){
  72. exitFlag = false;
  73. try{
  74. sms1 = ex1.exchange("тобi пiзда",50,TimeUnit.MILLISECONDS);
  75. }catch (TimeoutException ex){}
  76. try{
  77. sms2 = ex2.exchange("тобi пiзда",50,TimeUnit.MILLISECONDS);
  78. }catch (TimeoutException ex){}
  79. break;
  80. }
  81. if (i % (SECONDS_DELAY * 1000 / 50) == 0 && interrupt) { // 10 seconds
  82. System.out.printf("Calculations take longer than expected. Do you want to continue?\ny - yes, yy - continue and never ask again, other - terminate\n");
  83. String str = keyboard.next();
  84. switch (str) {
  85. case "y":
  86. break;
  87. case "yy":
  88. interrupt = false;
  89. break;
  90. default:
  91. exitFlag = true;
  92. //fThread.interrupt();
  93. try{
  94. sms1 = ex1.exchange("тобi пiзда",50,TimeUnit.MILLISECONDS);
  95. }catch (TimeoutException ex){}
  96. try{
  97. sms2 = ex2.exchange("тобi пiзда",50,TimeUnit.MILLISECONDS);
  98. }catch (TimeoutException ex){}
  99. break;
  100. }
  101. }
  102. i = ++i % (SECONDS_DELAY * 1000 / 50);
  103. } catch (InterruptedException e) {
  104. e.printStackTrace();
  105. } while(((fThread.isAlive() || gThread.isAlive()) && !exitFlag) && !(fThread.done && gThread.done));//fThread.result * gThread.result != 0
  106.  
  107. if(!exitFlag)
  108. System.out.println("f(x)&&g(x) = " + (fThread.result && gThread.result));
  109. }
  110. }
  111.  
  112. class FThread extends Thread {
  113.  
  114. private int x;
  115. public volatile boolean result = true;
  116. public volatile boolean done = false;
  117. Exchanger<String> exchanger;
  118. String sms;
  119.  
  120.  
  121. FThread(int _x , Exchanger ex){
  122. super();
  123.  
  124. exchanger = ex;
  125. x = _x;
  126. }
  127.  
  128. public void Read(){
  129. try{
  130. String mess = this.isAlive() ? "I am alive!" : "зараз як уїбу сука";
  131. sms = exchanger.exchange(mess);
  132. if(sms.equals("тобi пiзда"))this.interrupt();
  133. }catch (InterruptedException ex){
  134. System.out.println(ex.getMessage());
  135. }
  136. }
  137. private boolean f(int x) throws InterruptedException {
  138. for(int i=0; i<500; i++) {
  139. if(interrupted())
  140. throw new InterruptedException();
  141. sleep(50);
  142. Read();
  143. }
  144. done = true;
  145. return x > 10;
  146. }
  147.  
  148. @Override
  149. public void run(){
  150. try {
  151. result = f(x);
  152. } catch(InterruptedException ex){
  153. return;
  154. }
  155. }
  156. }
  157.  
  158. class GThread extends Thread {
  159.  
  160. private int x;
  161. public volatile boolean result = true;
  162. public volatile boolean done = false;
  163. Exchanger<String> exchanger;
  164. String sms;
  165. Random rnd = new Random();
  166.  
  167. GThread(int _x , Exchanger ex){
  168. super();
  169.  
  170. exchanger = ex;
  171. x = _x;
  172. }
  173.  
  174. public void Read(){
  175. try{
  176. String mess = this.isAlive() ? "I am alive!" : "зараз як уїбу сука";
  177. sms = exchanger.exchange(mess);
  178. if(sms.equals("тобi пiзда"))this.interrupt();
  179. }catch (InterruptedException ex){
  180. System.out.println(ex.getMessage());
  181. }
  182. }
  183.  
  184. private boolean g(int x) throws InterruptedException {
  185.  
  186. /* for(int i=0; i<505; i++) {
  187. if(interrupted())
  188. throw new InterruptedException();
  189. sleep(50);
  190. Read();
  191. }*/
  192. for(int i = 0 ; i < 50 ; ++i){
  193. Read();
  194. if(interrupted())
  195. throw new InterruptedException();
  196. if((rnd.nextInt() % 100) == x){
  197. done = true;
  198. return true;
  199. }
  200. }
  201.  
  202. done = true;
  203. return false;
  204. // return x > 20;
  205. }
  206.  
  207. @Override
  208. public void run(){
  209. try {
  210. result = g(x);
  211. } catch(InterruptedException ex){
  212. return;
  213. }
  214. }
  215. }
  216.  
  217. class funcThread extends Thread {
  218.  
  219. private int x;
  220. public volatile boolean result = true;
  221. public volatile boolean done = false;
  222. Exchanger<String> exchanger;
  223. String sms;
  224. Random rnd = new Random();
  225.  
  226. funcThread(int _x , Exchanger ex){
  227. super();
  228.  
  229. exchanger = ex;
  230. x = _x;
  231. }
  232.  
  233. public void Read(){
  234. try{
  235. String mess = this.isAlive() ? "I am alive!" : "зараз як уїбу сука";
  236. sms = exchanger.exchange(mess);
  237. if(sms.equals("тобi пiзда"))this.interrupt();
  238. }catch (InterruptedException ex){
  239. System.out.println(ex.getMessage());
  240. }
  241. }
  242.  
  243. public boolean g(int x) throws InterruptedException {
  244.  
  245. return false;
  246. // return x > 20;
  247. }
  248.  
  249. @Override
  250. public void run(){
  251. try {
  252. result = g(x);
  253. } catch(InterruptedException ex){
  254. return;
  255. }
  256. }
  257. }
  258.  
  259. class F extends funcThread{
  260. // private int x;
  261. // public volatile boolean result = true;
  262. // public volatile boolean done = false;
  263. // Exchanger<String> exchanger;
  264. // String sms;
  265.  
  266. F(int _x , Exchanger ex){
  267. super(_x , ex);
  268. // x = _x;
  269. // exchanger = ex;
  270. }
  271.  
  272. @Override
  273. public boolean g(int x) throws InterruptedException{
  274. for(int i = 0 ; i < 50 ; ++i){
  275. Read();
  276. if(interrupted())
  277. throw new InterruptedException();
  278. if((rnd.nextInt() % 100) == x){
  279. done = true;
  280. return true;
  281. }
  282. }
  283.  
  284. done = true;
  285. return false;
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement