java多线程获取数据问题

m0_57396996 2021-05-05 01:34:47
代码已经搞完,但是想知道在一个java程序中,能否自定义数据,将数据传到线程中让线程运行。
例如:
import java.util.*;
import java.util.ArrayList;
import java.util.Scanner;

/*class shuru{
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
System.out.print("请输入线程数量,按回车结束");
int n=sc.nextInt();
System.out.print("请输入卷子数量,按回车结束");
int T=sc.nextInt();}
}*/


public class shareThread implements Runnable{
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
System.out.print("请输入线程数量,按回车结束");
int n=sc.nextInt();
System.out.print("请输入卷子数量,按回车结束");
int T=sc.nextInt();}


//public static int n = 10000; //数组内数字的数量
//public static int T = 5; //线程数
public static int[] ary = new int[n];
public static int[] newAry = new int[n]; //排序后的数组

//线程运行
public void run(){
//System.out.println(ary[i]);
//System.out.println(Thread.currentThread().getName());


synchronized(shareThread.class){
for(int i = 1; i <= T; i++){
if(Thread.currentThread().getName().equals("thread"+i)){
int[] newArray= work(i);
int count = n/T *(i-1);
for(int j = 0;j<newArray.length;j++){
newAry[count] = newArray[j];
//System.out.println(++count);
//System.out.print(newArray[j]+ " ");
//System.out.println(newAry[count] + "");
count++;
}
}
}
}
}
上述标红的这段能否实现?
...全文
75 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

51,395

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧