私はJavaRMIベースのプロジェクトに取り組んでいますClient-->Job Scheduler--> Server structure
。
以下に示すように、ジョブスケジューラクラスには2つのメソッドがあります。コメントは、コードの各行の目的を説明しています。
private ConcurrentLinkedQueue<Job> jobQueue;
private ConcurrentLinkedQueue<ComputeServerRef> serverQueue;
private final Object lock = new Object();
/**
* Accepts newly generated job from client and schedule it with idle Compute
* Server or queue it in JobQueue depending on the availability of idle
* Compute Server in serverQueue.
*
* @param job Job object
*
* @exception RemoteException
* Thrown if any remote remote error occurred.
*/
public Job acceptJob(Job job) throws RemoteException
{
// Report a "Job scheduled" to all Loggers.
eventGenerator.reportEvent(new JobSchedulerEvent
("Job "+job.getJobName()+" scheduled"));
synchronized(lock)
{
while(true)
{
if (!serverQueue.isEmpty())
{
// If serverQueue is not empty then get one server from it,
// remove it from the server queue and assign it a new job.
ComputeServerRef csr = serverQueue.poll();
try
{
job = csr.performJob(job);
updateServerStatus(csr);
break;
}
catch(RemoteException re)
{
continue;
}
}
else
{
jobQueue.add(job);
try{
Thread.currentThread().wait();
}catch(InterruptedException e){
e.printStackTrace();
System.out.println("Thread Interrupted");
}
// Check if it's the turn of current thread's job.
// If not then goto wait();
while (!jobQueue.peek().getJobName().equals(job.getJobName()))
{
try{
Thread.currentThread().wait();
}catch(InterruptedException e){
e.printStackTrace();
System.out.println("Thread Interrupted");
}
}
job=jobQueue.poll();
}
}
}
return job;
}
/**
* Adds newly started compute server to serverQueue
*
* @param csr reference of the remote object ComputeServer.
*
* @exception RemoteException
* Thrown if any remote remote error occurred
*/
public void updateServerStatus(ComputeServerRef csr)throws RemoteException
{
synchronized(lock)
{
serverQueue.add(csr);
Thread.currentThread().notifyAll();
}
}
wait()
のメソッドの最初の呼び出しでIllegalMonitorStateExceptionが発生しacceptJob()
ます。任意のアイデア、これを解決する方法。
ありがとう、Jiten