当前位置: 代码迷 >> Java相关 >> 这题目没有看明白!
  详细解决方案

这题目没有看明白!

热度:342   发布时间:2007-10-16 05:57:14.0
这题目没有看明白!
write a program that shares an integer array between 2 separate thread. each thread is given a segment of the array to use and may not write outside its given segment. one of the thread should initializeits values to random number in the rang 1..100 and the other thread should toss a coin N times and record the result of each toss( n equals the segment sive ).the main program should wait for the threads to finish and print the values of the array前面是要两个线程吧数组分成两半,之后又要跑硬币,随即数了!
搜索更多相关的解决方案: separate  between  outside  thread  number  

----------------解决方案--------------------------------------------------------
回复:(yynn)这题目没有看明白!

我想可能实这样的:
两个独立的线程共享一个整数数组,该数组分成两端,分别被两个线程访问。其中一个线程把自己的那一段的数据初始化为1到100的随机数,另一个线程抛硬币,记录数值,分别填入数组中,(其实就是把自己的那段数组数据初始化为0和1的随机数而已)。
个人理解


----------------解决方案--------------------------------------------------------
写一个程序使两个线程共享一个整数数组。每个线程都能使用该数组的一部分,但是不能写各自那部分以外的部分。其中一个线程用范围在1--100的随机数初始化它的那部分数据,另一个线程抛N次硬币并记录每次的结果来写它的那部分数据。主程序应该等待线程结束,并打印出数组的值。
----------------解决方案--------------------------------------------------------

class RandomNumber implements Runnable{
private int n[]=new n[50];
Random r = new Random();
int b = r.nextInt(100);
public void run(){
for(int j=0 ; j<100; j++){
System.out.println(""+n[]);
try{
sleep(100);
}catch(InterruptedException e){}
}
}

}

class Toss implements Runnable{
private int m[] = new m[50];
Random s = new Random();
int a = s.nextInt(2);
public void run(){
for(int j=0 ; j<100; j++){
System.out.println(""+n[j]);
try{
sleep(100);
}catch(InterruptedException e){}
}
}
}

class Q1 {
public static void main(String args[]){
Runnable c1 = new RandomNumber(50);
Runnable c2 = new Toss(50);
new Thread(c1).start();
new Thread(c2).start();
}
}


----------------解决方案--------------------------------------------------------
我这样编写错误很多有没有人帮我修改一下啊!本事菜鸟,刚学,但是以上来就碰上thread估计有一堆奇怪的错误!请大师指点了!
----------------解决方案--------------------------------------------------------

你那样写是两个线程 但他们访问的是两个不同的 array

public class demo{
private int m[] = new m[50];
//定义 ,赋值。。。

new Thread(){ //线程1

public void run(){//实现}

}

}.start();
new Thread(){ //线程2

public void run(){//实现}

}

}.start();
}


----------------解决方案--------------------------------------------------------

如果这样的话貌似不能访问了,是private私有的!


----------------解决方案--------------------------------------------------------
回复:(yynn)如果这样的话貌似不能访问了,是privat...
import java.util.Random;
public class Test{
public static void main(String[] args)throws Exception{
int[] a = new int[10];
T1 t1 = new T1(a,0,4);
T2 t2 = new T2(a,5,9);
Thread a1 = new Thread(t1);
Thread b1 = new Thread(t2);
a1.start();
b1.start();
Thread.sleep(1000);//---等到初始化完成,再打印
for(int i:a)
System.out.print(i+" ");
}
}
//------初始化为1到100的进程
class T1 implements Runnable{
private int[] a;
private int start;//进程能访问的数组的起始
private int end;
public T1(int[] a,int start ,int end){
this.a = a;
this.start = start;
this.end = end;
}
public void run(){
Random r = new Random();
for(int index = start;index <= end;index++){
a[index] = 1 + r.nextInt(100);
}
}
}
//----------抛硬币的线程
class T2 implements Runnable{
private int[] a;
private int start;
private int end;
public T2(int[] a,int start ,int end){
this.a = a;
this.start = start;
this.end = end;
}
public void run(){
Random r = new Random();
for(int index = start;index <= end;index++){
a[index] = r.nextInt(2);
}
}
}
----------------解决方案--------------------------------------------------------
感觉题目理解是不是一个数组分一半,然后先初始化一半数组,然后跑硬币猜数组!
----------------解决方案--------------------------------------------------------
  相关解决方案