Skip to content

Instantly share code, notes, and snippets.

@Vox1oot
Created February 24, 2014 14:16
Show Gist options
  • Save Vox1oot/9189133 to your computer and use it in GitHub Desktop.
Save Vox1oot/9189133 to your computer and use it in GitHub Desktop.
package com.javarush.test.level17.lesson06.task02
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