All pastes #674475 Raw Edit

Miscellany

public text v1 · immutable
#674475 ·published 2007-08-29 08:51 UTC
rendered paste body
package melo.risorse_timeout;

import java.util.*;

public class Gestore
{
        private boolean []resources;//true if resource is assigned, false if it's free
        private int []waiting;//counts the number of processes waiting for a specific resource
        private boolean []outoftime;//true if the Timer has set it up to true for timeout
        private int dim;//number of resources
        private int count=0;//counter of assigned resources
        private int nProc;//number of processes
        
        public Gestore(int dim, int nProc)
		{
			this.dim=dim;
			this.nProc=nProc;
			resources = new boolean[dim];
			waiting = new int[dim];
			for(int i=0; i<dim; i++)
			{
				resources[i]=false;
				waiting[i]=0;
			}
			outoftime = new boolean[nProc];
			for(int i=0; i<nProc; i++)
				outoftime[i]=false;
		}
        
        .
        .
        class MyTask extends TimerTask
		{
		    int proc,res=-1;
		
		    public MyTask(int proc, int res)
		    {
				this.proc=proc;
				this.res=res;
			}
		    public void run()
		    {
		        timeout(proc,res);
		    }
		
		}
		
		public synchronized void timeout(int proc, int res)
		{
			outoftime[proc]=true;
			notifyAll();
		}

		public synchronized void request(int proc, int res, long timeout) throws TimeoutException
		{
			//wait
			if(resouces[res])
			{
				waiting[res]++;
				outoftime[proc]=false;
				Timer timer = new Timer();
				TimerTask task = new MyTask(proc,res);
				timer.schedule(task,timeout);
				while(resources[res]==true && outoftime[proc]==false)
				{
					try{ wait(); } catch(InterruptedException e){}
				}
				if(outoftime[proc])
				{
					timer.cancel();
					m.Exit();
					throw new TimeoutException();
				}
				timer.cancel();
				waiting[res]--;
			}
			resouces[res]=true;
			count++;
		}		

}