java 多线程访问数组求和?
有两个数组int[]input={100,5,98,77,60,88,36};int[]output={0,0,0,0,0,0,0};例如:如果取input的100,那么...
有两个数组
int[] input={100,5,98,77,60,88,36};
int[] output={0,0,0,0,0,0,0};
例如:如果取input的100,那么就是计算1+。。。+100=5050;要把得到的这个值放到output的第一个数组元素中,要input跟output要一一对应,用3到5个线程,写出。
请写具体,谢谢! 展开
int[] input={100,5,98,77,60,88,36};
int[] output={0,0,0,0,0,0,0};
例如:如果取input的100,那么就是计算1+。。。+100=5050;要把得到的这个值放到output的第一个数组元素中,要input跟output要一一对应,用3到5个线程,写出。
请写具体,谢谢! 展开
2个回答
展开全部
public class T
{
int[] input={100,5,98,77,60,88,36};
int[] output={0,0,0,0,0,0,0};
int curr = 0;
public synchronized int getCurr() {
if( curr== input.length )
return -1;
return curr++;
}
static class Worker extends Thread
{
T t;
public Worker(T t) {
this.t = t;
}
public void run() {
int idx;
while( (idx=t.getCurr()) != -1 ) {
int sum = 0;
for(int i=0; i<t.input[idx]; i++ ) {
sum += i;
}
t.output[idx] = sum;
}
System.out.println( getName() + " over" );
}
}
public static void main(String[] args) throws Exception{
T t = new T();
Thread t1 = new Worker(t);
Thread t2 = new Worker(t);
Thread t3 = new Worker(t);
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
for(int i=0; i<t.output.length; i++) {
System.out.print( t.output[i] + "," );
}
System.out.println();
}
}
追问
这个for循环要改成
for(int i=0; i<=t.input[idx]; i++ ) {
sum += i;
}
呵呵!
谢谢!
给力!
展开全部
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class T
{
int[] input={100,5,98,77,60,88,36};
int[] output={0,0,0,0,0,0,0};
int curr = 0;
public synchronized int getCurr() {
if( curr== input.length )
return -1;
return curr++;
}
static class Worker extends Thread
{
T t;
public Worker(T t) {
this.t = t;
}
public void run() {
int idx;
while( (idx=t.getCurr()) != -1 ) {
int sum = 0;
for(int i=0; i<t.input[idx]; i++ ) {
sum += i;
}
t.output[idx] = sum;
}
System.out.println( getName() + " over" );
}
}
public static void main(String[] args) throws Exception{
T t = new T();
Thread t1 = new Worker(t);
Thread t2 = new Worker(t);
Thread t3 = new Worker(t);
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
for(int i=0; i<t.output.length; i++) {
System.out.print( t.output[i] + "," );
}
System.out.println();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class T
{
int[] input={100,5,98,77,60,88,36};
int[] output={0,0,0,0,0,0,0};
int curr = 0;
public synchronized int getCurr() {
if( curr== input.length )
return -1;
return curr++;
}
static class Worker extends Thread
{
T t;
public Worker(T t) {
this.t = t;
}
public void run() {
int idx;
while( (idx=t.getCurr()) != -1 ) {
int sum = 0;
for(int i=0; i<t.input[idx]; i++ ) {
sum += i;
}
t.output[idx] = sum;
}
System.out.println( getName() + " over" );
}
}
public static void main(String[] args) throws Exception{
T t = new T();
Thread t1 = new Worker(t);
Thread t2 = new Worker(t);
Thread t3 = new Worker(t);
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
for(int i=0; i<t.output.length; i++) {
System.out.print( t.output[i] + "," );
}
System.out.println();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询