我有下面的代码试样片,问题的同步
public class test implements Runnable{
public void run()
{
synchronized(this)
{
for(int i=0;i<10000;i++)
System.out.println(i);
}
}
public static void main(String args[]) throws Exception
{
Thread t=new Thread(new test());
t.start();
//Thread.sleep(100);
t.suspend();
System.out.println("Hi");
}
}
我有以下的要求,当线程被挂起它必须执行该循环的所有迭代而不会挂起。我觉得同步是为了这个目的,但它没有解决问题。当我挂起一个线程,如果它在一个同步块中,那么它必须在块被执行后才被挂起。什么java结构可以用于这个?
注意的可能性Thread.suspend ()已被弃用。 http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#suspend() – 3urdoch 2011-04-16 19:52:44