Java 多线程的正确实现
我已经思考了几天了,我相信我对多线程工作原理的思考是有缺陷的。我已经查阅了并发API,但仍然没有得到我的问题的答案 如果我有一个进程想要返回一个唯一的字符串,并且我想要100个不同的线程来运行这个进程(从而给我100个线程和100个不同的字符串),我可以利用executorServices来完成这个任务,对吗 然后,一旦每个线程都有一个字符串,我想将该字符串发送到一个队列,从而用消息爆破队列(同样,听起来像是executorService.submit()调用的用法) 最后,一旦一个线程将消息发送到队列,我希望这个线程立即开始检查另一个队列的响应(匹配它的唯一字符串),如果它匹配,则输出一些数据并终止 虽然我的印象是使用Executorservice将是解决我的问题的答案,但我在实现上失败了。我是否还缺少另一个解决方案,或者使用此方法进行多线程处理就足够了?如果是,如何解决 迄今为止的代码。在意识到我的问题后,我在sendTextMessage后停止:Java 多线程的正确实现,java,multithreading,executorservice,Java,Multithreading,Executorservice,我已经思考了几天了,我相信我对多线程工作原理的思考是有缺陷的。我已经查阅了并发API,但仍然没有得到我的问题的答案 如果我有一个进程想要返回一个唯一的字符串,并且我想要100个不同的线程来运行这个进程(从而给我100个线程和100个不同的字符串),我可以利用executorServices来完成这个任务,对吗 然后,一旦每个线程都有一个字符串,我想将该字符串发送到一个队列,从而用消息爆破队列(同样,听起来像是executorService.submit()调用的用法) 最后,一旦一个线程将消息发
int arraySize = 750; //Number of hits to send out: MANIPULATE THIS IF YOU WANT TO SEND MORE
// String[] toSend = new String[arraySize];
String[] rfids = new String[arraySize];
double total = 0;
int count = 0;
//Insert Connection information and set up clients here//
clientSend.connectSend();
clientRec.connectRec(); //edit to ensure credientials are corrects
// System.out.println("Signed-in");
StringBuffer output = new StringBuffer(); //What holds out output
File infile = new File("infile.txt"); //Populating Rfids array
Scanner scan = new Scanner(infile);
for (int i = 0; i <= arraySize-1; i++)
{
if(scan.hasNextLine())
{
rfids[i]=scan.nextLine();
// System.out.println(rfids[i]);
}
}
scan.close();
count=0;
ExecutorService load = Executors.newFixedThreadPool(arraySize);
Callable<String> readAndSendPrep = () ->
{
StringBuffer fileBasedResponse = new StringBuffer();
String rfid = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("input.txt")); //This is the standard message that will be sent everytime, give or take
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String line; //temp var
for(int x = 0; x<arraySize-1;x++)
{
try {
while ((line = reader.readLine ()) != null)
{
if (line.trim().startsWith("<MessageId>"))
{
// System.out.println(rf);
rfid = rfids[arraySize]; //not necessary i think
int endIndex = line.trim().indexOf("</MessageId>");
String messageId = line.trim().substring(11, endIndex);
line = "<MessageId>" + messageId + " - " + rfids[arraySize] + "</MessageId>"; //puts unique ID in thread details
}
else if (line.trim().startsWith("str"))
{
// System.out.println(allRFID[thisIndex]);
rfid = rfids[arraySize];
line = "str" + rfids[arraySize] + "str"; //Another unique ID
// System.out.println("BOOM");
}
fileBasedResponse.append(line); //put the whole response to the StringBuffer object
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Thread.sleep(1);
return fileBasedResponse.toString();
};
TimeUnit.SECONDS.sleep(10);
Future<String> fileBasedResponse = load.submit(readAndSendPrep);
while(!fileBasedResponse.isDone())
{
Thread.sleep(1);
}
String fileBasedResponseStr = fileBasedResponse.toString();
Runnable sender = () ->
{
try {
clientSend.sendTextMessage(fileBasedResponseStr);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
clientSend.close(); //close connections
clientRec.close();
System.out.println(output.toString()); //output the results
System.out.println(count);
int-arraySize=750//要发送的点击数:如果要发送更多,请对此进行操作
//String[]toSend=新字符串[arraySize];
String[]rfid=新字符串[arraySize];
双倍合计=0;
整数计数=0;
//在此处插入连接信息并设置客户端//
clientSend.connectSend();
clientRec.connectRec()//编辑以确保信笺正确无误
//系统输出打印项次(“签到”);
StringBuffer输出=新的StringBuffer()//什么能维持产出
File infle=新文件(“infle.txt”)//填充rfid数组
扫描仪扫描=新扫描仪(填充);
对于(int i=0;i
{
StringBuffer fileBasedResponse=新的StringBuffer();
字符串rfid=“”;
BufferedReader reader=null;
试一试{
reader=new BufferedReader(new FileReader(“input.txt”);//这是每次发送的标准消息
}捕获(FileNotFoundException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
字符串行;//临时变量
对于(int x=0;x
{
试一试{
clientSend.sendTextMessage(fileBasedResponseStr);
}捕获(JME){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
};
clientSend.close();//关闭连接
clientRec.close();
System.out.println(output.toString());//输出结果
系统输出打印项次(计数);
您应该发布一些可验证的代码example@ThomasCook我明白了,编辑后添加了代码。看看java.util.concurrent.ExecutorCompletionService,它可能会对您有所帮助。此外,任务与线程不同。您可以有700个任务,但只有10个线程来完成它们。使用ThreadPoolExecutor
,您将获得大部分开箱即用的东西。