黑马程序员技术交流社区
标题:
帮忙解答下注释的地方
[打印本页]
作者:
在学野马
时间:
2014-10-1 21:45
标题:
帮忙解答下注释的地方
本帖最后由 在学野马 于 2014-10-2 09:13 编辑
public class xian implements Runnable{
private static int[] arr = new int[6];
private static int index = 0 ;
public static void main(String[] args) throws Exception{
new Thread(new xian()).start();
new Thread(new xian()).start();
Thread.sleep(2000);
for(int i : arr){
System.out.print(i+"\t"+"aaaaa");
}
}
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=0;i<3;i++){ //我已经定义了输出三个数 为什么不管用
int number = (int)(Math.random()*10+1);
synchronized (xian.class) {
arr[index] = number;
System.out.println(arr[index]+"bbbbbbbb");
index++;
}
}
}
}
复制代码
作者:
wtjohn
时间:
2014-10-1 22:49
两个线程分别输出了3个数啊{:3_46:}
作者:
在学野马
时间:
2014-10-1 22:55
wtjohn 发表于 2014-10-1 22:49
两个线程分别输出了3个数啊
你发个截图
作者:
wtjohn
时间:
2014-10-1 23:15
{:3_58:} 基础我也没看完呢...有点迷糊...不过你这个.应该是两个线程,不管有没有获得锁,都已经是在for循环里了..所以就生成了6个随机数,对吧? 是不是这个情况
作者:
wtjohn
时间:
2014-10-1 23:20
public class xian implements Runnable{
private static int[] arr = new int[6];
private static int index = 0 ;
public static void main(String[] args) throws Exception{
new Thread(new xian()).start();
new Thread(new xian()).start();
Thread.sleep(2000);
for(int i : arr){
System.out.print(i+">>>a ");
}
}
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=0;i<3;i++){ //我已经定义了输出三个数 为什么不管用
synchronized (xian.class) {
if(arr[i]==0)
{
int number = (int)(Math.random()*10+1);
arr[index] = number;
System.out.println(arr[index]+":::::::::::b");
index++;
}
}
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2