需要帮助理解Java中的线程吗
好的,我正在创建一个Java程序,用于下载游戏的MOD需要帮助理解Java中的线程吗,java,multithreading,Java,Multithreading,好的,我正在创建一个Java程序,用于下载游戏的MODMinecraft。我有下面的类,它完成了所有的下载工作,并且运行良好。但是我有一个问题,在mod下载时进度条不更新(整个程序在下载时冻结) 经过大量研究,其他人似乎也有同样的问题,可以通过使用线程来解决 我只需要知道是否可以使用我现有的代码来实现这一点,还是必须完全重新编写我的ThisJava类 package com.anarcist.minemodloader; import java.io.BufferedInputStream;
Minecraft
。我有下面的类,它完成了所有的下载工作,并且运行良好。但是我有一个问题,在mod下载时进度条不更新(整个程序在下载时冻结)
经过大量研究,其他人似乎也有同样的问题,可以通过使用线程来解决
我只需要知道是否可以使用我现有的代码来实现这一点,还是必须完全重新编写我的ThisJava类
package com.anarcist.minemodloader;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.JProgressBar;
public class modsDownloader {
public static Boolean downloadMod(String mod, String saveLoc, JProgressBar progress){
try {
URL url = new URL(mod);
//Set ProgressBar to 0%
progress.setValue((int)0);
//Open the Connection to the file
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//Get filesize
int filesize = connection.getContentLength();
//Start reading at byte 0
float totalDataRead = 0;
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
FileOutputStream fos = new FileOutputStream(saveLoc);
BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int i=0;
while((i = in.read(data, 0, 1024)) >= 0){
totalDataRead = totalDataRead + i;
bout.write(data,0,i);
float Percent=(totalDataRead * 100) / filesize;
progress.setValue((int)Percent);
}
bout.close();
in.close();
}catch(Exception e){
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null,e.getMessage(), "Error",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
return true;
}
}
此外,使用以下代码调用此函数/类:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Object selectedObj = modsList.getSelectedValue();
if (selectedObj instanceof modsListItem) {
try {
modsListItem selectedItem = (modsListItem) selectedObj;
System.out.println(selectedItem.getValue());
String fullName = selectedItem.getValue();
String fileParts[] = fullName.split("/");
String fileName = fileParts[fileParts.length - 1];
String saveLoc = config.getModsFolder(true) + "/" + fileName;
modsDownloader.downloadMod(selectedItem.getValue(), saveLoc, jProgressBar1);
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这个方法调用是您的,并在一个单独的线程中传递参数,但是return true语句是在主线程中执行的,因此,尽管您的线程尚未结束其工作,它可能会返回true
public static Boolean downloadModThread(String mod, String saveLoc, JProgressBar progress){
new Thread(
new Runnable(){
public void run(){
downloadMod(mod, saveLoc, progress);
}
}
).start();
return true;
}
您可以使用SwinginvokeAndWait()或invokeLater()概念,如FIFO逻辑来执行特定作业,并在作业完成后触发某些内容 您可以使用SwingWorker逻辑执行长时间运行的任务,还可以定期更新UI 提及
参考显然,你必须重写其中的一些内容。Read@jbnize我读过这篇文章,我希望我能从
doInBackground
函数调用我的downloadMod
函数。我已经尝试过如何实现这些参数,但并不完全确定。这就是我的想法,axcept您可能不会使用doInBackground()方法中的swing组件(如进度条)。如果您需要这方面的帮助,请发布您尝试过的代码。扔掉它并使用ProgressMonitorInputStream。