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();
}
}
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();
}
}
- relates to
-
JDK-1234319 Win32 only: program with multiple threads crashes after running for a while
- Closed