Created
February 24, 2014 14:16
-
-
Save Vox1oot/9189133 to your computer and use it in GitHub Desktop.
package com.javarush.test.level17.lesson06.task02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level17.lesson06.task02; | |
/* Предложения | |
Не используя synchronized сделайте так, чтобы количество сделанных и принятых предложений было одинаковым. | |
*/ | |
public class Solution { | |
public static volatile int proposal = 0; | |
public static void main(String[] args) { | |
new MakeProposal().start(); | |
new AcceptProposal().start(); | |
} | |
public static class MakeProposal extends Thread { | |
@Override | |
public void run() { | |
int thisProposal = proposal; | |
while (proposal < 10) { | |
System.out.println("Сделано предложение №" + (thisProposal + 1)); | |
proposal = ++thisProposal; | |
try { | |
Thread.sleep(100); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
public static class AcceptProposal extends Thread { | |
@Override | |
public void run() { | |
int thisProposal = proposal; | |
while (thisProposal < 10) { | |
if (thisProposal != proposal) { | |
System.out.println("Принято предложение №" + proposal); | |
thisProposal = proposal; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment