문제

I have one thread that prints the elapsed time each second from the start of execution and another thread that prints a message every fifteen seconds. The first thread should update a time variable that is shared between threads and will notify other threads to read the time variable each time it updates the time variable. This is what I have currently:

public class PingPong implements Runnable
{ 
    private static final int REPETITIONS = 4;

String curName = "";
int currentTime = 0;

Thread t1;
Thread t2;

PingPong() {
    t1 = new Thread(this, "Admin");
    t1.start();

    t2 = new Thread(this, "Admin1");
    t2.start();

}

public void run () {
try {
curName = Thread.currentThread().getName();

if(curName.equals(t1.getName())){
    for (int i=1; i<REPETITIONS; i++) { 
         System.out.println(Thread.currentThread().getName() + ": " + i);
        Thread.sleep(1000);
        // System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().getState());
        System.out.println(t1.getName());
        currentTime++;
    }
}
/*else if(curName == t2){
    System.out.println("Thread 2");
}*/
System.out.println(currentTime);    


} catch (InterruptedException e) {      
         return; // end this thread
}
}

public static void main(String[] args) {
  new PingPong();
}
}

I am very new to threads and I'm not sure if I am implementing what I already have correctly. Also, I have no idea how to notify another thread. I feel like I am not on the right path at the moment.

If anyone has any help, it is really appreciated!

도움이 되었습니까?

해결책

Try this code :

class T1 extends Thread
{
   private SharedClass s;
   private int t;
   T1 (SharedClass s)
   {
      this.s = s;
   }

   public void run ()
   {
       while(true) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        s.setSharedTime (++t);
        System.out.println (t + " displayed by T1");
       }
   }
}

class T2 extends Thread {
    private SharedClass s;

       T2 (SharedClass s)
       {
          this.s = s;
       }

       public void run ()
       {
          while(true) { 
          int t;
          t = s.getSharedTime ();
          System.out.println (t + " displayed by T2 after 15 seconds.");
          }
       }
}

public class SharedClass {
private int time;
private boolean shareable = true;

public static void main(String[] args) {
    SharedClass s = new SharedClass ();
    new T1 (s).start ();
    new T2 (s).start ();
}
synchronized void setSharedTime (int c)
{
   while (!shareable)
      try
      {
         wait ();
      }
      catch (InterruptedException e) {}

   this.time = c;
   if(c%15==0)
   shareable = false;
   notify ();
}

synchronized int getSharedTime ()
{
   while (shareable)
      try
      {
         wait ();
      }
      catch (InterruptedException e) { }

   shareable = true;
   notify ();

   return time;
}
}

The threads in java are lightweight processes and are shared by a single CPU. And also the java statements takes some time to execute. This program is not guaranteed to run in exactly 15 seconds time interval but it is approximately 15 seconds.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top