皆を助けてください。私はjavaで同期コードを書いた。 2つのクラスがあります。最初のクラスは1000を増やし、2番目のクラスは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();
}
}
}
}
この宿題はありますか? –
のように見える... –
私はちょうどいくつかのexsercicesに取り組んで...そして私はデッドロックに立ち往生した。 – PKb