BG Development


: (7) [1] 2 3 ...  »  ( ) Reply to this topicStart new topicStart Poll

> Java Threads
Kyo
: 22-10-2015, 22:04
Quote Post



:
:
:

: 80
: 21.09.14



,
:

CODE

import java.util.Random;

class Athlete extends Thread {

   private int num;

   public Athlete(int num) {
       this.num = num;
   }

   @Override
   public void run() {
       synchronized (RelaySim.relay) {

           try {
               RelaySim.relay.wait();
           } catch (InterruptedException ex) {
           }
           
           
           System.out.println("Starting " + this.num);

           try {
               Thread.sleep(((new Random()).nextInt(3) + 9) * 1000);
           } catch (InterruptedException ex) {
           }

           
           System.out.println("Finishing " + this.num);
           
           RelaySim.relay.notify();
       }
   }
}

public class RelaySim {

   public static final Object relay = new Object();

   public static void main(String[] args) {

       Athlete a[] = new Athlete[4];

       for (int i = 0; i < 4; i++) {
           a[i] = new Athlete(i);
           a[i].start();
           try {
               Thread.sleep(1);
           } catch (InterruptedException ex) {
           }
       }

       synchronized (relay) {
           relay.notify();
       }
   }
}


. 4 9 11 .
, ..
"" Thread.sleep(1); . . - .
PMEmail Poster
Top
felore
: 22-10-2015, 22:11
Quote Post



:
:
:

: 3872
: 22.10.09



, . , .


--------------------
PMEmail Poster
Top
Kyo
: 22-10-2015, 22:22
Quote Post



:
:
:

: 80
: 21.09.14




public static final Object relay = new Object();

, ..
, wait notify.
sleep , deathlock
PMEmail Poster
Top
ivan84
: 23-10-2015, 07:37
Quote Post



:
:
:

: 8170
: 01.04.11



QUOTE (Kyo @ 22-10-2015, 23:22)

public static final Object relay = new Object();

. .
(4-) . .

---
CODE
public class RelaySim {
public static void main(String args[]) {
for(int i = 0; i < 10; i++) {
new Athlete();
}

AthletesManager.start();
}
}


public class Athlete implements Runnable {
private final Object relay = new Object();

private volatile boolean alive = false;
private Thread thread;

private String name;
private static int counter;

public Athlete() {
name = "Athlete " + ++counter;
AthletesManager.add(this);

thread = new Thread(this);
thread.start();
}

public void go() {
synchronized(relay) {
relay.notify();
}
}

public boolean isAlive() {
return alive;
}

@Override
public void run() {
try {
System.out.println(toString() + " ready");

synchronized(relay) {
alive = true;
relay.wait();
}

System.out.println(toString() + " start");
}
catch(InterruptedException e) {
e.printStackTrace();
}
finally {
System.out.println(toString() + " finish");
AthletesManager.next();
}
}

@Override
public String toString() {
return name;
}
}

public class AthletesManager {
private static Queue<Athlete> athletes = new LinkedList<>();

public static void add(Athlete athlete) {
if(athlete == null) {
throw new IllegalArgumentException();
}

athletes.add(athlete);
}

private static boolean isAlive() {
for(Athlete athlete: athletes) {
if(!athlete.isAlive()) {
return false;
}
}

return true;
}

public static void start() {
while(!isAlive());

next();
}

public static void next() {
Athlete athlete = athletes.poll();

if(athlete != null) {
athlete.go();
}
}
}


QUOTE
Athlete 1 ready
Athlete 3 ready
Athlete 4 ready
Athlete 2 ready
Athlete 5 ready
Athlete 6 ready
Athlete 7 ready
Athlete 8 ready
Athlete 9 ready
Athlete 10 ready
Athlete 1 start
Athlete 1 finish
Athlete 2 start
Athlete 2 finish
Athlete 3 start
Athlete 3 finish
Athlete 4 start
Athlete 4 finish
Athlete 5 start
Athlete 5 finish
Athlete 6 start
Athlete 6 finish
Athlete 7 start
Athlete 7 finish
Athlete 8 start
Athlete 8 finish
Athlete 9 start
Athlete 9 finish
Athlete 10 start
Athlete 10 finish


ivan84 23-10-2015, 10:22
PMEmail Poster
Top
ivan84
: 23-10-2015, 10:32
Quote Post



:
:
:

: 8170
: 01.04.11



.
CODE
public class RelaySim {
      public static final Object relay = new Object();
      
      public static void main(String args[]) {
            for(int i = 0; i < 10; i++) {
                  new Athlete();
            }
            
            AthletesManager.start();
      }
}

public class Athlete implements Runnable {            
      private volatile boolean alive = false;
      private Thread thread;
      
      private boolean notify = false;
      
      private String name;
      private static int counter;
      
      public Athlete() {
            name = "Athlete " + ++counter;
            AthletesManager.add(this);
            
            thread = new Thread(this);      
            thread.start();
      }
      
      public void go() {
            notify = true;
            
            synchronized(RelaySim.relay) {
                  RelaySim.relay.notifyAll();
            }
      }
      
      public boolean isAlive() {
            return alive;
      }
      
      @Override
      public void run() {
            try {
                  System.out.println(toString() + " ready");

                  do {
                        synchronized(RelaySim.relay) {
                              alive = true;
                              RelaySim.relay.wait();
                        }
                  }
                  while(!notify);
                  
                  System.out.println(toString() + " start");
            }
            catch(InterruptedException e) {
                  e.printStackTrace();
            }
            finally {
                  System.out.println(toString() + " finish");
                  AthletesManager.next();
            }
      }

      @Override
      public String toString() {
            return name;
      }
}

public class AthletesManager {
      private static Queue<Athlete> athletes = new LinkedList<>();
      
      public static void add(Athlete athlete) {
            if(athlete == null) {
                  throw new IllegalArgumentException();
            }
            
            athletes.add(athlete);
      }
      
      private static boolean isAlive() {
            for(Athlete athlete: athletes) {
                  if(!athlete.isAlive()) {
                        return false;
                  }
            }
            
            return true;
      }
      
      public static void start() {            
            while(!isAlive());
            
            next();
      }
      
      public static void next() {
            Athlete athlete = athletes.poll();
            
            if(athlete != null) {
                  athlete.go();
            }
      }
}


ivan84 23-10-2015, 10:40
PMEmail Poster
Top
felore
: 23-10-2015, 12:43
Quote Post



:
:
:

: 3872
: 22.10.09



. . http://pastebin.com/ZLurhpyB

: / "".

felore 23-10-2015, 12:48


--------------------
PMEmail Poster
Top
ivan84
: 23-10-2015, 12:59
Quote Post



:
:
:

: 8170
: 01.04.11



. , .
PMEmail Poster
Top
felore
: 23-10-2015, 14:12
Quote Post



:
:
:

: 3872
: 22.10.09



.. , , ?


--------------------
PMEmail Poster
Top
ivan84
: 23-10-2015, 14:33
Quote Post



:
:
:

: 8170
: 01.04.11



. "" . .
. , . .

ivan84 23-10-2015, 14:34
PMEmail Poster
Top
felore
: 23-10-2015, 15:44
Quote Post



:
:
:

: 3872
: 22.10.09



, , .


--------------------
PMEmail Poster
Top
0 (0 , 0 )
, :
« | Java | »

Topic Options : (7) [1] 2 3 ...  » Reply to this topicStart new topicStart Poll

 


Copyright © 2003-2018 | BG Development | All Rights Reserved
RSS 2.0