harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Vera Volynets (JIRA)" <j...@apache.org>
Subject [jira] Commented: (HARMONY-1939) [drlvm][classlib] loadClass method in ClassLoader is not synchronized
Date Tue, 10 Apr 2007 11:39:33 GMT

    [ https://issues.apache.org/jira/browse/HARMONY-1939?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12487753
] 

Vera Volynets commented on HARMONY-1939:
----------------------------------------

Alexey, after a investigation I've wrote a test which hangs on RI and on DRLVM because of
deadlock caused by synchronized loadClass(). 
Since it behaves the same on both machines the solution proposed by you is acceptable.
The test:
public class Test {
    static Object lock = new Object();
    static Object second_lock = new Object();
    static int readyNum = 0;

    public static void main(String[] args) throws Exception {
	TestThread tt1 = new TestThread("first");
	System.out.println("first thread: " + tt1.getName());
	TestThread tt2 = new TestThread("second");
	System.out.println("second thread: " + tt2.getName());	
	tt1.start();
	tt2.start();    
    }    
    static TestClassLoader cl = new TestClassLoader();
    static class TestClassLoader extends ClassLoader {
	private byte[] classData = new byte[] {
	    -54, -2, -70, -66, 0, 0, 0, 49, 0, 13,
		10, 0, 3, 0, 10, 7, 0, 11, 7, 0,
		12, 1, 0, 6, 60, 105, 110, 105, 116, 62,
		1, 0, 3, 40, 41, 86, 1, 0, 4, 67,
		111, 100, 101, 1, 0, 15, 76, 105, 110, 101,
		78, 117, 109, 98, 101, 114, 84, 97, 98, 108,
		101, 1, 0, 10, 83, 111, 117, 114, 99, 101,
		70, 105, 108, 101, 1, 0, 14, 84, 101, 115,
		116, 67, 108, 97, 115, 115, 46, 106, 97, 118,
		97, 12, 0, 4, 0, 5, 1, 0, 9, 84,
		101, 115, 116, 67, 108, 97, 115, 115, 1, 0,
		16, 106, 97, 118, 97, 47, 108, 97, 110, 103,
		47, 79, 98, 106, 101, 99, 116, 0, 33, 0,
		2, 0, 3, 0, 0, 0, 0, 0, 1, 0,
		1, 0, 4, 0, 5, 0, 1, 0, 6, 0,
		0, 0, 29, 0, 1, 0, 1, 0, 0, 0,
		5, 42, -73, 0, 1, -79, 0, 0, 0, 1,
		0, 7, 0, 0, 0, 6, 0, 1, 0, 0,
		0, 1, 0, 1, 0, 8, 0, 0, 0, 2,
		0, 9 };  

	protected Class findClass(String name) throws ClassNotFoundException {
            System.out.println("Notify thread for second_lock " + Thread.currentThread().getName());

	    synchronized (second_lock){
	        second_lock.notifyAll();
	    }
	    try{
	    	System.out.println("Let this thread to sleep a bit: " + Thread.currentThread().getName());
	        Thread.currentThread().sleep(1000);
	    } catch(Throwable e) {}

            System.out.println("Before synch of lock in findClass, should go in deadlock!
" + Thread.currentThread().getName());	    
	    synchronized(lock) {
		if (name.equals("TestClass")) {
		    return defineClass(null, classData, 0, classData.length);
		} else {
		    throw new ClassNotFoundException("Class " + name + " not found.");
		}	
	    }
	}
    }

    static class TestThread extends Thread {
        public TestThread(String name){
	    super(name); 
	}
	public void run() {
	    try {
		readyNum++;
		if(readyNum == 1) {
		System.out.println("Thread is BEFORE synch loadClass " + Thread.currentThread().getName());
		    synchronized(lock)
		    {
		    	synchronized(second_lock) {
			    second_lock.wait();
			}
			System.out.println("Thread is IN synch before loadClass " + Thread.currentThread().getName());
			cl.loadClass("TestClass");
			System.out.println("Thread is AFTER loadClass, class is loaded, but shouldn't " + Thread.currentThread().getName());

		    }
		} else {
		    System.out.println("Thread is BEFORE simple loadClass " + Thread.currentThread().getName());
	
		    cl.loadClass("TestClass");
		    System.out.println("Thread is AFTER loadClass, class is loaded, but shouldn't " + Thread.currentThread().getName());
		}    
	    } catch (Exception ex) {
		ex.printStackTrace();
	    }
	}    
    }
}

> [drlvm][classlib] loadClass method in ClassLoader is not synchronized
> ---------------------------------------------------------------------
>
>                 Key: HARMONY-1939
>                 URL: https://issues.apache.org/jira/browse/HARMONY-1939
>             Project: Harmony
>          Issue Type: Bug
>          Components: DRLVM
>         Environment: IA32, WinXP
>            Reporter: Mikhail Markov
>         Assigned To: Alexey Varlamov
>            Priority: Critical
>
> The following code is reproducing the following situation: 2 threads simultaneously run
Class.forName() method for the same classname and the same test classloader. It is expected
that findClass() method of the test classloader should be called only once otherwise this
lead to double call of defineClass() method (which is not allowed, see also HARMONY-1932).
> Output on RI:
> Test passed.
> Output on Harmony:
> Test failed: findClass was called 2 times.
> -------------Test.java---------------
> public class Test {
>     static TestClassLoader cl = new TestClassLoader();
>     static Object lock = new Object();
>     static int readyNum = 0;
>     static int findClassCalled = 0;
>     public static void main(String[] args) throws Exception {
>         TestThread tt1 = new TestThread();
>         TestThread tt2 = new TestThread();
>         tt1.start();
>         tt2.start();
>         while (readyNum < 2) {
>             Thread.sleep(100);
>         }
>         synchronized (lock) {
>             lock.notifyAll();
>         }
>         tt1.join();
>         tt2.join();
>         if (cl.findClassCalled() != 1) {
>             System.out.println("Test failed: findClass was called "
>                     + cl.findClassCalled() + " times.");
>         } else {
>             System.out.println("Test passed.");
>         }
>     }
>     static class TestClassLoader extends ClassLoader {
>         private int findClassCalled;
>         /*
>          * Byte array of bytecode equivalent to the following source code:
>          *     public class TestClass {
>          *     }
>          */
>         private byte[] classData = new byte[] {
>             -54, -2, -70, -66, 0, 0, 0, 49, 0, 13,
>             10, 0, 3, 0, 10, 7, 0, 11, 7, 0,
>             12, 1, 0, 6, 60, 105, 110, 105, 116, 62,
>             1, 0, 3, 40, 41, 86, 1, 0, 4, 67,
>             111, 100, 101, 1, 0, 15, 76, 105, 110, 101,
>             78, 117, 109, 98, 101, 114, 84, 97, 98, 108,
>             101, 1, 0, 10, 83, 111, 117, 114, 99, 101,
>             70, 105, 108, 101, 1, 0, 14, 84, 101, 115,
>             116, 67, 108, 97, 115, 115, 46, 106, 97, 118,
>             97, 12, 0, 4, 0, 5, 1, 0, 9, 84,
>             101, 115, 116, 67, 108, 97, 115, 115, 1, 0,
>             16, 106, 97, 118, 97, 47, 108, 97, 110, 103,
>             47, 79, 98, 106, 101, 99, 116, 0, 33, 0,
>             2, 0, 3, 0, 0, 0, 0, 0, 1, 0,
>             1, 0, 4, 0, 5, 0, 1, 0, 6, 0,
>             0, 0, 29, 0, 1, 0, 1, 0, 0, 0,
>             5, 42, -73, 0, 1, -79, 0, 0, 0, 1,
>             0, 7, 0, 0, 0, 6, 0, 1, 0, 0,
>             0, 1, 0, 1, 0, 8, 0, 0, 0, 2,
>             0, 9 };
>         protected Class findClass(String name) throws ClassNotFoundException {
>             findClassCalled++;
>             try {
>                 synchronized (lock) {
>                     lock.wait();
>                 }
>             } catch (InterruptedException ie) {
>             }
>             if (name.equals("TestClass")) {
>                 return defineClass(null, classData, 0, classData.length);
>             } else {
>                 throw new ClassNotFoundException("Class " + name + " not found.");
>             }
>         }
>         int findClassCalled() {
>             return findClassCalled;
>         }
>     }
>     static class TestThread extends Thread {
>         public void run() {
>             try {
>                 readyNum++;
>                 Class.forName("TestClass", false, cl);
>             } catch (Exception ex) {
>                 ex.printStackTrace();
>             }
>         }
>     }
> }
> ------------------------------------------

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message