【问题标题】:Creating threads to download a file and make a counter java创建线程以下载文件并制作计数器 java
【发布时间】:2014-11-24 12:27:08
【问题描述】:

我正在尝试制作一个下载文件的程序。下载文件时,需要有一个从头到尾计数的基本计数器。这两个函数需要在它们自己的线程中。我创建了一个方法/线程来下载文件。我无法弄清楚如何制作基本计数器。没什么特别的,但它需要计数并且只显示当前数字。任何帮助将不胜感激。

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static myutilites.methods.*;


public class Downloader
{

public static void main(String[] args) throws IOException
{
    String filename = "Nexus5MahdiRom.zip";
    URL link = new URL(
    "http://files.mahdi-rom.com/OnePlus%20One/"
    + "OnePlus%20One%20Builds/mahdi-2.7-bacon-20140903.zip");

    Thread t1 = new Thread(new Runnable()
    {
        @Override
        public void run() 
        {
            try 
            {
                downloadFile(filename,link);
            } catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    });

    Thread t2 = new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            try {
                counter(link, t1);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    t1.start();
    println("Downloading File...");
}

public void getScreenSize()
{
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getScreenSize();
    println("width = " + d.width + " height = " + d.height);
}
public static void downloadFile(String fileName, URL link) throws IOException
{
    InputStream in = new BufferedInputStream(link.openStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte [] buf = new byte[1024];
    int n = 0;
    int count = 0;

    while (-1 !=(n=in.read(buf)))
    {
        out.write(buf, 0, n);
    }
    println(count);
    out.close();
    in.close();

    byte[] response = out.toByteArray();

    FileOutputStream fos = new FileOutputStream(fileName);
    fos.write(response);
    fos.close();

    println("Download Finished");
}
public static void locate(int row, int col)
{
    System.out.print("\033["+row+";"+col+"H");
}
public static int counter(URL link, Thread t1) throws IOException
{
    int count = 0;
    link.openConnection();
    while(t1.isAlive())
    {
        count++;
        return count;
    }
    return count;
}
}

【问题讨论】:

    标签: java multithreading inputstream


    【解决方案1】:

    AtomicInteger 上使用getAndIncrementincrementAndGet,这是一个线程安全的计数器(计数器初始化为0,如果两个线程同时调用incrementAndGet,那么您可以保证计数器将返回1到一个线程和 2 到另一个线程,最终状态为 2)

    【讨论】:

    • 我试过了,但是当我打印到屏幕上时,它只保留显示数字。我希望新号码删除旧号码。我不希望在下载过程中显示整个计数。只有一个数字,删除那个数字,然后是下一个数字。
    • @user3242607 在定义filenamelink 的相同位置定义AtomicInteger,并将其作为参数传递给counter。将 counter 更改为 void 方法,因为您通过引用传递 AtomicInteger
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多