Small example of livelock regression in garbage collector for GCJ 3.3 under Win32

Øyvind Harboe oyvind.harboe@zylin.com
Tue May 20 17:34:00 GMT 2003


Just to narrow down things a bit.

The exception does not have to take place in the Object.finalize()
method.


It seems that this is not a trivial fix adding an explicit check for
a null pointer, since that still leaves the division by
zero exception and ...


public class Test
{
	public String replaceAll(String in, String a, String b)
	{
		String t = "";
		int i;
		for (i = 0; i < (in.length() - a.length() + 1); i++)
		{
			String c = in.substring(i, i + a.length());

			if (c.equals(a))
			{
				t += b;
				i = i + a.length() - 1;
			} else
			{
				t += in.substring(i, i + 1);
			}
		}
		if (i < in.length())
		{
			t += in.substring(i, in.length() - 1);
		}
		return t;
	}

	int y;

	public void run()
	{

		for (int i = 0; i < 4; i++)
		{
			final String name = "thread #" + i + " ";
			Thread tr = new Thread(new Runnable()
			{
				public void run()
				{
					stressGB(name);
				}
			});
			tr.start();
		}

	}

	public static void main(String[] args)
	{
		new Test().run();

	}

	void stressGB(String thread)
	{
		char[] abs = { 'a', 'b', 'c', 'd' };
		String foo = "";
		for (int i = 0; i < 100000000; i++)
		{
			if ((i % 1000) == 0)
			{
				System.out.println(thread + " " + i + ""
+ foo);
			}

			try
			{
				if (Math.random()>0.0001)
				{
					System.out.println("Exception
this " + 1000/y); 
				}
			}
			catch (Throwable e)
			{
			}

			double r;
			r = Math.random();

			foo = foo + abs[(int) (((r * (abs.length -
1))))];
			r = Math.random();

			foo =	replaceAll(foo, "" + abs[(int) (((r
*(abs.length - 1))))], "");
		}
	}
}



More information about the Java mailing list