简体   繁体   English

如何将特定线程尽快处理为多线程Java?

[英]How to process particular thread as fast into multithread java?

I am doing multithread processing for generating some products... i will create the products every 10 minutes..i have 2 threads .. first thread will wakeup every 10 minutes... the second thread will generate products... The problem is in second thread...because in second thread ,I will generate some product files(files created by bytes ,all files size will be 30 or 50 mb)..After creating product files,product will be generated by depending on those product files.The product files are created every 10 minutes(it should be created every 10 minutes)..but its taking time more than 10 minutes to create those product files...so can u pls anyone tell me the way to create the files as very fast? 我正在执行多线程处理以生成某些产品...我将每10分钟创建一次产品..我有2个线程..第一个线程每10分钟唤醒一次...第二个线程将生成产品...问题是在第二个线程中...由于在第二个线程中,我将生成一些产品文件(按字节创建的文件,所有文件大小将为30或50 mb)。创建产品文件后,将根据这些产品文件生成产品产品文件每10分钟创建一次(应该每10分钟创建一次)。但是创建这些产品文件需要花费超过10分钟的时间...所以您能请任何人告诉我创建文件的方式非常快?

public class Scheduler implements Runnable{
    Thread firstThread;
    public Scheduler() {
        firstThread = new Thread(this,"timer");
    }
    public void run() {

        do {
            if(wakeUptime) {//if wake up 10.00 correct time
                secondThread(wakeUptime);
                sleep(10mins);
            } else {
                // calculate next round of next ten mins...
                // if this process starts at 10.05A.M ,
                // it will sleep 5 mins
                // (round of time in 10 minutes(10minutes - 5 mins))
                // so sleep(5 mins)
            }

        } while(true);

    }

    secondThread(final string time) {
        Runnable r = new Runnable() {
            firstFunction(time);
            secondFunction(time);
        };
        Thread t = new Thread(r);
        try {
            t.start();
            t.join;//its getting very slow..how to make fast...
        } catch(Exception e) {
        }

    }

    firstFunction(String time) {

        // Here, files will be created every 10 minutes
        // total files size 40 or 50 mb...
        // files created by using bytes.
        // the problem is creating files taking more than 10 minutes...
        // so is there any method to create files very fast?

    }

    secondFunction(String time) {
        // generate product
    }

}

in the first method,the files will be created..the file size will be 40 or 50 mb...so its taking lot of time to create the files... the files are creating more than 10 minutes...Is there any method to create files as very fast? 在第一种方法中,将创建文件。文件大小为40或50 mb ...因此创建文件需要花费大量时间...文件创建时间超过10分钟...有什么方法可以非常快速地创建文件?

First, i'm recommend to use power production ready sheduler - Quartz from Terracota, he has a trigger system where you can define misfire behavior. 首先,我建议使用Terracota的可发电的Sheduler- Quartz ,他有一个触发系统,您可以在其中定义失火行为。 Second, of course optimize your code where you create files with productrs - there is no miracles in world, if you cat't create source data for function, you can't run you function with right result 其次,当然要在与生产人员一起创建文件的地方优化代码-世界上没有奇迹,如果您不创建函数的源数据,就无法以正确的结果运行函数

You say "files created by using bytes" - are you writing one byte at a time to the file? 您说“使用字节创建的文件”-您一次要向一个文件写入一个字节吗? Have you wrapped your FileOutputStream in a BufferedOutput stream? 您是否将FileOutputStream包装在BufferedOutput流中? Doing so could make an order of magnitude or more improvement. 这样做可能会提高一个数量级或更多。

If you're using Java 5 or higher, you can use a ScheduledExecutor to run the second thread. 如果您使用的是Java 5或更高版本,则可以使用ScheduledExecutor运行第二个线程。 This will probably be more reliable than reimplementing it on your own. 这可能比您自己重新实现更可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM