class notifying
{
public static void main(String arg[])
{
telephone currentph=new telephone();
currentph.init_telphone(5);
int phone_code=123456;
while(true)
{ try
{
notify notify_thread=new notify();
notify_thread.set_telephone(currentph);
notify_thread.set_phone_code(phone_code++);
notify_thread.start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
/*-------------设备类-------------------------------------------------*/
class telephone
{
int notify_TDcode=0;
int[] TD_state;
public void init_telphone(int notify_TDcode) /*初始化当前设备*/
{
this.notify_TDcode=notify_TDcode;
TD_state=new int[notify_TDcode];
for(int i=0;i<TD_state.length;i++)
{
TD_state[i]=0;/*0可用 1不可用*/
}
System.out.println("inital telephone........complete");
}
public int get_notify_state(int TD_index) /*得到当前通道状态*/
{
return TD_state[TD_index];
}
public int get_notify_TDcode()/*得到当前设备的通道总数*/
{
return notify_TDcode;
}
public void set_notify_state(int TD_index,int state)/*设置当前通道状态*/
{
TD_state[TD_index]=state;
}
}
/*-------------------------多线程分配通道-------------------------------------*/
class notify extends Thread /*每电话分配一个独立线程,占用一个通道*/
{
telephone current_telephone;
int phone_code;
public void set_telephone(telephone current_telephone)/*设置当前对应的设备*/
{
this.current_telephone=current_telephone;
}
public void set_phone_code(int phone_code)
{
this.phone_code=phone_code;
}
public void run()
{
int Current_notify_td=0;
while(true)
{
synchronized(current_telephone)/*分配通道号*/
{
Current_notify_td=Current_notify_td%current_telephone.get_notify_TDcode();
if(current_telephone.get_notify_state(Current_notify_td)==0)
{
current_telephone.set_notify_state(Current_notify_td,1); //使用当前通道//
System.out.println("start....notify...通道号分配完成:"+Current_notify_td);
break;
}
Current_notify_td++;
}
}
/*通道分配完成。。开始通知操作*/
start_notify(Current_notify_td);
}
public void start_notify(int Current_notify_td) /*开始拨号线程*/
{
Cstart_notify start_notify_thread=new Cstart_notify(Current_notify_td,current_telephone,phone_code);
start_notify_thread.start();
}
}
/*拨号线程*/
class Cstart_notify extends Thread
{
int use_TD;
telephone current_telephone;
int phone_code;
public Cstart_notify(int use_TD,telephone current_telephone,int phone_code)
{
this.use_TD=use_TD;
this.phone_code=phone_code;
this.current_telephone=current_telephone;
}
public void run()
{
System.out.println("使用通道号为:"+use_TD+"开始拨号。。。。。。。。。。"+phone_code);
Cstart_Code start_code_thread=new Cstart_Code(use_TD,current_telephone,phone_code);
start_code_thread.start();
}
}
/*通知该号码线程*/
class Cstart_Code extends Thread
{
int use_TD;
telephone current_telephone;
int phone_code;
public Cstart_Code(int use_TD,telephone current_telephone,int phone_code)
{
this.use_TD=use_TD;
this.phone_code=phone_code;
this.current_telephone=current_telephone;
}
public void run()
{
System.out.println("使用通道号为:"+use_TD+"通知该号码:"+phone_code);
free_TD free_TD_class=new free_TD(use_TD,current_telephone);
free_TD_class.start();
}
}
class free_TD extends Thread
{
int use_TD;
telephone current_telephone;
public free_TD(int use_TD,telephone current_telephone)
{
this.use_TD=use_TD;
this.current_telephone=current_telephone;
}
public void run()
{
synchronized(current_telephone)
{
current_telephone.set_notify_state(use_TD,0);
System.out.println("回收通道号:"+use_TD);
}
}
}