📄 cdma_cd_cline.java
字号:
class CDMA_CD
{
//从新写RUN函数
public static void main (String args[])
{
MyThread cp1= new MyThread(100);
MyThread cp2= new MyThread(200);
cp1.start();
if(cp1.isAlive())
{
System.out.println("the computer 100 start");
}
try
{MyThread.sleep(200);
}
catch(InterruptedException e)
{System.out.println("sleep error.!!!!!!");
}
cp2.start();
if(cp2.isAlive())
{
System.out.println("the computer 200 start");
}
}
}
class MyThread extends Thread
{
private int cp_id;
private int num;
int num_cash;
private int bus;
public MyThread(int s)
{
num_cash=num=bus=0;
cp_id=s;
}
public void run()
{
try
{
while(true)
{
if(bus==0)
{
System.out.println("no one is using the load, so the compputer "+cp_id+" will set information! ");
bus = cp_id;
}
if(bus==cp_id)
{
Thread.sleep(2000);
num++;
System.out.println("it is eontention period,and it is the "+num_cash+" one! the computer no. is "+cp_id);
this.Mysleep(Truncated(num_cash));
if(bus==100)
{bus=200;}
else
{
bus=100;
}
}
else
{
num_cash++;
if(num_cash>16)
break;
System.out.println("it is eontention period,and it is the "+num_cash+" one! the computer no. is "+cp_id);
this.Mysleep(Truncated(num_cash));
}
}
}catch(InterruptedException e)
{
System.out.println("thread has wrong");
}
System.out.println("the thread exit");
}
private void Mysleep(int sec)
{
try
{
sleep(sec);
}catch(Exception e)
{
System.out.println("sleepError:"+e);
}
}
//退避算法
public int Truncated(int l)
{
int j=power(2,l);
java.util.Random r=new java.util.Random();
int sec=r.nextInt(j);
return sec;
}
//求幂函数
int power(int i,int j)
{
int value=1;
for(;j>0;j--)
{
value=value*i;
}
return value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -