public void startFtp(String address, int port, String username, String password, String filepath, String file)
{
//...parsing parameter to class variables
try {
while (download) {
downloadAndBroadcast();
}
} catch (Exception e){
Log.d(TAG, "startFtp disconnected or fails");
e.printStackTrace();
}
}
private void downloadAndBroadcast() {
beginTime = System.currentTimeMillis();
try {
URL url = new URL("http://" + serverAddress + "/" + serverFile);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
inputStream = new BufferedInputStream(url.openStream());
long difference;
byte data[] = new byte[4094];
int count;
while ((count = inputStream.read(data)) != -1 && download) {
downloadCount += count;
long stoptime = System.currentTimeMillis();
difference = stoptime - beginTime;
if (difference > 1000 && download) {
currentSpeed = downloadCount / (difference / 1000L);
averageSpeed = (averageSpeed + currentSpeed) / 2;
broadcastSpeed();
downloadCount = 0;
beginTime = stoptime;
}
}
clearInputStream();
} catch (Exception e) {
Log.d(TAG, "FAIL");
e.printStackTrace();
} finally {
clearInputStream();
Log.d(TAG, "downloadAndBroadcast: finally");
}
}
private void broadcastSpeed() {
Log.d(TAG, "broadcastSpeed: ");
toMainActivityIntent = new Intent(Constants.BROADCAST_SPEED)
.addCategory(Intent.CATEGORY_DEFAULT)
.putExtra("speed", averageSpeed)
.putExtra("thread", String.valueOf(Thread.currentThread().getId()));
downloadService.sendBroadcast(toMainActivityIntent); //send to main activity, in main activity, there is a listener that analyzes Broadcast and Intent
}
private void clearInputStream() {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void stopDownload() {
Log.d(TAG, "stopDownload: ");
download = false;
}
[Shutting down, pool size = 4, active threads = 4, queued tasks = 0, completed tasks = 0]
如何启动线程:
int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();
executor = new ThreadPoolExecutor(
NUMBER_OF_CORES * 2,
NUMBER_OF_CORES * 2,
60L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>()
);
executor.execute(new Runnable() {
public void run() {
FTPDownloader ftpDownloader = new FTPDownloader(downloadService);
ftpDownloader.startFtp(server.getServer(), server.getPort(), server.getUser(), server.getPass(), server.getPath(), server.getFiledl());
if (Thread.interrupted()) {
Log.d(TAG, "run: ");
ftpDownloader.stopDownload();
}
}
});
我调用executor.shutdownnow();
关闭线程:
private class MainActivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
executor.shutdownNow();
}
}
请尝试以下步骤:
去掉这些台词:
if (Thread.interrupted()) {
Log.d(TAG, "run: ");
ftpDownloader.stopDownload();
}
在downloadandbroadcast
中,在}catch(异常e){
之前添加此内容
} catch (InterruptedException ie) {
// catching IE will reset interrupted status. So just clear the flag.
download = false;
}
@在创建时覆盖受保护的无效(捆绑保存的InstanceState){ menifest文件:
我的问题是JPA/Hibernate在调用entityManager时返回true。getTransaction()。isActive(),即使我没有显式启动事务(请参阅下面的代码)。 这里的问题是,我想从数据库中读取一些内容,在这种情况下,SerializationException是可以的,因为这只是表明持久化对象不再适合实际代码,需要重新计算。下面的代码不只是返回null,而是引发以下异常:
单步执行似乎使任务注册工作成功,没有任何例外。然后返回visual studio,在任务的方法的第一行添加断点,然后转到“调试位置”工具栏,单击向下箭头并选择。几秒钟后,visual studio退出(我的应用程序也退出)。 有人看到我做错了什么导致后台任务失败吗?
我已经用Java编写了一个简单的ECHO协议服务器,使用。 很简单: 线程正在上等待客户端的输入。但是我发现所有的工作线程都处于状态,通过,我认为它们可能处于状态。 线程在等待数据到达时被阻塞,但为什么在这里它是可运行的? 下面是输出:
问题内容: 锁定后总是会出现try / finally块,为什么? 为什么要尝试/最终阻止而不是简单地编写如下代码: 问题答案: 万一出了什么问题(抛出异常等),您无论如何都要确保释放了锁。这只是标准做法,即使在技术上在这种情况下也没有必要。
我一直在使用线程向链接发送GET请求(一切正常)。然而,我需要它异步运行,所以我创建了一个新线程并运行了它。问题是我需要它在线程执行完毕后返回值。我用