Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zad3;
- public class TextsStandard {
- boolean newTxt = false;
- String txt = null;
- volatile boolean isFirstDone = false;
- long timeStart = 0;
- long timeEnd = 0;
- synchronized String getText() {
- while (newTxt == false)
- try {
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- newTxt = false;
- notifyAll();
- return txt;
- }
- synchronized void setText(String s) {
- while (newTxt == true)
- try {
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- newTxt = true;
- txt = s;
- notifyAll();
- }
- public void start() {
- timeStart = System.nanoTime();
- }
- public void finish() {
- timeEnd = System.nanoTime();
- System.out.println("Czas ze standardową synchronizacją (w nanosekundach): " + new Long(timeEnd - timeStart));
- }
- }
- public class StandardA extends Thread {
- TextsStandard texts=null;
- public StandardA()
- {
- texts=new TextsStandard();
- }
- @Override
- public void run() {
- new StandardB(texts).start();
- texts.start();
- for (int i=0;i<1000;i++)
- {
- texts.setText("a");
- }
- texts.isFirstDone=true;
- synchronized(System.out) {
- System.out.println(texts.isFirstDone);
- }
- }
- }
- public class StandardB extends Thread {
- TextsStandard texts = null;
- public StandardB(TextsStandard input) {
- texts = input;
- }
- @Override
- public void run() {
- while (texts.isFirstDone == false) {
- texts.getText();
- }
- texts.finish();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement