Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-1234318

Win32 only: program that starts multiple threads does not exit

XMLWordPrintable

    • 1.1
    • x86
    • windows_nt
    • Not verified

      A program with multiple threads does not exit.

      Steps to reproduce
      compile and run the attached code

      class TwoThreadsTest {
         public static void main (String args[])
         {
            Semaphore sem = new Semaphore();
            SyncThread s1 = new SyncThread("Jamaica", sem);
            SyncThread s2 = new SyncThread1("Fiji", sem);

            s1.start();
            s2.start();

      /* Add this code to end the program
            try
            {
               s2.join();
            }
            catch (InterruptedException e) {}
      */
             
          }
      }

      class SyncThread extends Thread
      {
         Semaphore mySem;
         public SyncThread(String str, Semaphore sem)
         {
            super(str);
            mySem = sem;
         }

         public void run()
         {
            System.out.println(getName() + ".run()");
            System.out.println(getName() + " getLock()");
            if ( mySem.getLock(2000))
               System.out.println(getName() + " has semaphore");
            else
               System.out.println(getName() + " couldn't get semaphore");
            
         }

      }

      class Semaphore extends Object
      {
         boolean bLock = true;

         public synchronized boolean isLocked()
         {
            return bLock;
         }

         public synchronized boolean getLock(int iWait)
         {
            if (bLock == true)
            {
               try
               {
                  if (iWait < 0)
                     wait();
                  else
                     wait(iWait);
               } // end try
               
               catch (InterruptedException e)
               {
                  System.out.println(e.getMessage());
               }
         
            } // end if is locked

            // recheck lock, has it been released
            if (bLock != true)
            {
               bLock = true;
               return true;
            }
            else
               return false;

         } // end getLock();

         public synchronized void unLock()
         {
            bLock = false;
            notify();
         }
      }


      class SyncThread1 extends SyncThread
      {
         public SyncThread1(String name, Semaphore sem)
         {
            super(name, sem);
         }

         public void run()
         {
            System.out.println(getName() + ".run()");
            System.out.println(getName() + " sleep");
            
            try { sleep(3000); }
            catch (InterruptedException e) {}

            System.out.println(getName() + " unlock");
            mySem.unLock();

         }
      }

            tlindholsunw Timothy Lindholm (Inactive)
            kasmithsunw Kevin Smith (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: