多线程练习(java)

 public class TestThread
{
public static void main(String[] args)
{
RandomNumber r=new RandomNumber(); Thread1 t1=new Thread1(r);
Thread2 t2=new Thread2(r); Thread thread1=new Thread(t1);
Thread thread2=new Thread(t2); thread1.start();
thread2.start();
}
}
 public class RandomNumber
{
public int i;
public int num[]; RandomNumber()
{
i=(int)(Math.random()*100)+11;
num=new int[i]; for (int j=0;j<i;j++)
{
num[j]=(int)(Math.random()*100);
}
}
}
 //public class Thread1 extends Thread {
public class Thread1 implements Runnable { private int Thread1[];
Thread1(RandomNumber rn)
{
//Thread1 = new int[rn.i];
Thread1 = rn.num;
}
public void run()
{
for(int i=0; i<Thread1.length; i++)
{
for(int j=0; j<Thread1.length-i-1; j++)
{
if(Thread1[j] > Thread1[j+1])
{
int mid = Thread1[j];
Thread1[j] = Thread1[j+1];
Thread1[j+1] = mid;
}
}
}
for(int i=0; i<Thread1.length; i++)
{
System.out.println(Thread1[i]);
}
System.out.println("Thread1打印完成。");
}
}
 import java.util.Arrays;  

 public class Thread2 implements Runnable {  

     private int Thread2[];
Thread2(RandomNumber rn)
{
// Thread2 = new int[rn.num.length];
Thread2 = rn.num;
} public void run()
{
Arrays.sort(Thread2);
for (int i=0;i<Thread2.length;i++)
{
System.out.println(Thread2[i]);
}
System.out.println("Thread2打印完成");
}
}
上一篇:thinkphp如何一次性的上传多个文件,在文件域中可以多选?


下一篇:VS2015 新Web项目(C#6)出现CS1617错误的解决