0
。我用java写了一个同步代码。有两类,第一类增加1000,第二类增加10。这个过程应该重复100次。 虽然我写了循环循环,但它们不起作用。我有一个死锁同步,请大家帮忙启动循环周期
下面是代码:
public class thread
{
static int count = 100;
public static void main(String[] args)
{
Thread thread1 = new Thread(new XThread());
Thread thread2 = new Thread(new YThread());
thread1.start();
thread2.start();
synchronized(thread2)
{
thread2.notify();
}
}
}
class XThread extends Thread {
static long sum=0;
static int i;
synchronized public void run() {
sum=5+1000;
System.out.println(i+" "+"Thread 1"+" "+sum);
{
for(i= 0; i < lab5.count; i++)
{
try {
{
System.out.println("-----------");
this.wait();
}
}
catch (InterruptedException ex)
{
sum=sum+1000;
System.out.println(i+" "+"Thread 1"+" "+sum);
notify();
}
}
}
}
}
class YThread extends Thread
{
static long sum;
static int i;
synchronized public void run()
{
sum=5+10;
System.out.println(YThread.i+" "+"Thread 2"+" "+YThread.sum);
for(i=0; i < lab5.count; i++)
{
try
{
{
System.out.println("------------");
this.wait();
}
}
catch (InterruptedException ex)
{
sum=sum+10;
System.out.println(YThread.i+" "+"Thread 2"+" "+YThread.sum);
notify();
}
}
}
}
这是功课? – 2012-03-09 19:33:08
看起来像一个... – 2012-03-09 19:33:53
我只是工作的一些exsercices ...和我坚持了僵局。 – PKb 2012-03-09 19:37:30