声明一个共享数组,起两个线程,两个线程分别隔一段时间(可以写一个随机数),
给数组中添加数据,每一个线程为数组添加3个数据即可。
就问一下我的做法对不对?
import java.util.*;
/*声明一个共享数组,起两个线程,两个线程分别隔一段时间(可以写一个随机数),
给数组中添加数据,每一个线程为数组添加3个数据即可。
*/
public class Test7
{
public static void main(String[] args)
{
Test t = new Test();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.start();
t2.start();
}
static class Test implements Runnable
{
public synchronized void run()
{
Random r = new Random();
int x =0;
for(x=0;x<3;x++)
{
int[] m =new int[6];
m[x]=r.nextInt(10);
System.out.println(m[x]+".........."+Thread.currentThread().getName());
}
}
}
}
|
|