真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java代碼時(shí)間長(zhǎng)處理 java代碼運(yùn)行很慢怎么辦

JAVA開(kāi)發(fā)的 后臺(tái)程序處理太久,導(dǎo)致頁(yè)面返回時(shí)響應(yīng)不了怎么辦?

采用分段加載,先讓后臺(tái)響應(yīng)頁(yè)面,需要長(zhǎng)時(shí)間處理的代碼變?yōu)榫€(xiàn)程去處理,如果需要把長(zhǎng)時(shí)間處理后結(jié)果返回到響應(yīng)的頁(yè)面去,(servlet2.0后好像是支持的),如果不行,可以采用異步方式。

創(chuàng)新互聯(lián)專(zhuān)注于朝陽(yáng)企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站定制開(kāi)發(fā)。朝陽(yáng)網(wǎng)站建設(shè)公司,為朝陽(yáng)等地區(qū)提供建站服務(wù)。全流程按需定制開(kāi)發(fā),專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)

在JAVA中如何實(shí)現(xiàn)長(zhǎng)時(shí)間任務(wù)

摘要:在軟件開(kāi)發(fā)中,我們經(jīng)常面臨著處理長(zhǎng)時(shí)間任務(wù)的多線(xiàn)程編程問(wèn)題。在我們的ezOne平臺(tái)的開(kāi)發(fā)中就多處涉及到,如JPC數(shù)據(jù)服務(wù)JPC數(shù)據(jù)處理服務(wù)、報(bào)警聯(lián)動(dòng)、門(mén)禁系統(tǒng)等。本人在編寫(xiě)DEMO程序的過(guò)程中幾易其稿,煞費(fèi)心機(jī),但依然感覺(jué)有許多地方需要改進(jìn),為了減少多線(xiàn)程編程帶來(lái)的風(fēng)險(xiǎn),我嘗試翻譯整理了一個(gè)類(lèi)似問(wèn)題的解決方案框架以達(dá)到一勞永逸。 為了便于閱讀,保留原文。 一、問(wèn)題背景Quite often, you need a class to perform tasks like data processing, listening to events, or checking another class' activities during the application’s lifetime. To achieve this, you probably use threads with a set of locks and notifications. Java Thread API is well documented, but you need a great deal of code and experience to make your thread work properly and efficiently. You can avoid writing such classes from scratch every time you need them and build a more robust application by applying the framework we'll discuss in this article. 在應(yīng)用程序中我們經(jīng)常需要一個(gè)類(lèi)去完成像數(shù)據(jù)處理、監(jiān)聽(tīng)事件或檢查另一個(gè)類(lèi)的活動(dòng)等任務(wù)。為了達(dá)到這個(gè)目標(biāo),我們可能使用帶有一套鎖和消息通知的線(xiàn)程。JAVA 線(xiàn)程API已經(jīng)很好的文檔化,但為了使線(xiàn)程能夠正確而高效地運(yùn)行,程序員仍然需要豐富的編程經(jīng)驗(yàn)并編寫(xiě)大量的代碼。通過(guò)應(yīng)用本篇文章中討論的框架,程序員能夠避免忍受煎熬寫(xiě)大量的代碼,快速創(chuàng)建健壯的應(yīng)用程序。 二、長(zhǎng)時(shí)間運(yùn)行任務(wù)的程序框架Framework for long-running tasks The primary thing about a long-lived task is that it should somehow be kept running during the application lifetime. The right way to accomplish this is to provide a thread of execution for a particular task. You create a task as a thread or as an implementation of the java.lang.Runnable interface. If you implement Runnable, you can gain better object-oriented design and avoid the single-inheritance problems. You can also more efficiently manipulate with Runnable instances, for example, using a thread pool that usually needs a Runnable instance, not a thread, to run. 關(guān)于長(zhǎng)時(shí)間運(yùn)行的任務(wù)的主要事情是如何在應(yīng)用程序的生命期使它一直保持運(yùn)行。實(shí)現(xiàn)的恰當(dāng)方法是提供一個(gè)線(xiàn)程來(lái)執(zhí)行這個(gè)特定的任務(wù)。我們可以通過(guò)繼承Thread類(lèi)或?qū)崿F(xiàn)java.lang.Runnable接口來(lái)達(dá)到該目標(biāo)。如果采用實(shí)現(xiàn)Runnable接口的方式,就可以能夠獲得更好的面向?qū)ο蟮脑O(shè)計(jì),同時(shí)可以避免JAVA中的單繼承問(wèn)題。另外,我們也能更有效的處理Runnable實(shí)例(例如使用線(xiàn)程池通常需要一個(gè)Runnable實(shí)例而不是線(xiàn)程來(lái)運(yùn)行)。 The essence of the framework is the abstract class Worker ( Listing A), which implements the Runnable interface and provides the helper methods for efficient task handling. Some of the methods are fully implemented, like the run() method, but some are abstract and have to be filled by you. If you want to create a long-running class, you need only to extend the Worker class and implement several abstract methods. Let’s look at these methods in more detail. 框架的基礎(chǔ)是一個(gè)叫Worker的抽象類(lèi),它實(shí)現(xiàn)了Runnable接口,并提供了有效處理任務(wù)的好方法。這些方法有些已經(jīng)被實(shí)現(xiàn),如run()方法,但有些是抽象方法,開(kāi)發(fā)人員必須自己來(lái)實(shí)現(xiàn)。如果要?jiǎng)?chuàng)建一個(gè)長(zhǎng)時(shí)間運(yùn)行的類(lèi),你只需要繼承Worker類(lèi)并實(shí)現(xiàn)幾個(gè)抽象方法。讓我們看看這些方法的細(xì)節(jié)。 The run() method of the Worker class is designed to continuously execute the work() method until it is stopped. The work() method can be responsible for data processing, reaction to some event, file reading or writing, SQL execution, etc. It can throw an exception, so it is a good practice to propagate it and let the run() method handle it. Worker 類(lèi)的run()方法被設(shè)計(jì)成只要不停止運(yùn)行就持續(xù)的執(zhí)行work()方法。work()方法可以負(fù)責(zé)數(shù)據(jù)處理、事件響應(yīng)、文件讀寫(xiě)、,執(zhí)行SQL命令等操作。這樣work()方法能夠拋出異常,并將異常傳給run(),然后由run()方法來(lái)處理這些異常。 The run() method has two levels of try-catch clause: outside and inside the while-loop. The first try-catch clause is meant to catch all nonprogrammed exceptions and guarantee that the run() method never exits. The second clause will catch any kind of exceptions belonging to business logic and behave accordingly. If some waiting operation takes place in the work() method (e.g., waiting on an InputStream or a Socket), it is advisable to propagate an InterruptedException. The thing to keep in mind is that the work() method does not need to have any while-loop to keep it going as long as an application runs. The Worker does this for you. run()方法有內(nèi)外兩層try-catch語(yǔ)句:一層處于while-loop循環(huán)外,一層在while-loop循環(huán)內(nèi)。前一個(gè)try-catch用于捕獲非編程異常以確保run()方法不退出。后一個(gè)try-catch語(yǔ)句捕獲關(guān)于業(yè)務(wù)邏輯和相應(yīng)行為的各種異常。如果在work()方法中發(fā)生了一些等待操作(例如等待一個(gè)輸入流或一個(gè)Socket),拋出一個(gè)InterruptedException的方法是可取的。要記住的是只要應(yīng)用程序在運(yùn)行,work()方法不需要任何while-loop循環(huán)去維持它運(yùn)行,這一切由Worker代辦了。 When the run() method starts, it calls the prepareWorker() which is designed to prepare all resources needed for a long-running task (Listing A). In this method call, you can, for example, establish a database connection or open a file that will be used further. It is especially good to place here some blocking operations like opening a socket, because they will be done in a separate thread and thus will not block the main thread of execution. run()開(kāi)始時(shí),調(diào)用prepareWorker()方法來(lái)準(zhǔn)備長(zhǎng)時(shí)間運(yùn)行任務(wù)需要的所有資源(參考程序清單A)。例如 ,在這個(gè)方法中可以打開(kāi)一個(gè)將要用到的數(shù)據(jù)庫(kù)連接或文件。尤其對(duì)于那些像建立一個(gè)socket這樣的阻塞操作放在這兒是很好的。因?yàn)槿糇屗鼈冊(cè)谝粋€(gè)獨(dú)立的線(xiàn)程中運(yùn)行,則不會(huì)阻塞主線(xiàn)程的執(zhí)行。 The opposite of the previous method is the releaseWorker() which is called when the run() method is about to exit (Listing A). Here, you can put the code to dispose of system resources used by this task or to perform other cleanup. This method is similar to java.lang.Object.finalize(), but it is explicitly called before a thread terminates. 與前面方法相反的是releaseWorker(),它在run()方法準(zhǔn)備退出時(shí)被調(diào)用(參考程序清單A)。在該方法中你可以編寫(xiě)那些釋放系統(tǒng)資源或執(zhí)行其它清除動(dòng)作的代碼。該方法類(lèi)似于java.lang.Object.finalize(),但它在線(xiàn)程中止時(shí)被顯式的調(diào)用。 三、框架中的錯(cuò)誤處理機(jī)制Handling errors in the framework Another important method is the handleError(), which takes a java.lang.Throwable as a parameter. This method is called each time an error situation occurs within the run() method. It is up to you how to implement error handling. One way is to log errors and control task termination by calling halt() method (Listing A). 另一個(gè)重要的方法是handleError(),它帶有一個(gè)java.lang.Throwable的輸入?yún)?shù)。在run()方法每次發(fā)生錯(cuò)誤時(shí)調(diào)用這個(gè)方法。這依賴(lài)于你怎么實(shí)現(xiàn)錯(cuò)誤處理。方法之一是寫(xiě)錯(cuò)誤日志并通過(guò)調(diào)用halt()方法中止任務(wù)(參考程序清單A)。 The isCondition() method is used to tell whether execution of the work() method can be started, thus allowing granular control over a task. It is useful in event-triggered frameworks when execution of the work() method is pending until some condition?for example, a buffer is not empty?is fulfilled. In Worker’s implementation, the condition is checked upon a lock notification and periodically with a time interval you specify in the setTimeout() method (Listing A). If you don’t need any waiting blocks in a task, just make the isCondition() method always return true. isCondition()方法用于判斷work()方法是否能夠被執(zhí)行。因此允許細(xì)粒度地控制任務(wù)。這在事件觸發(fā)的框架中非常有用。當(dāng)work()方法的執(zhí)行條件未滿(mǎn)足時(shí),work方法將被掛起,直到條件完全滿(mǎn)足(例如,緩存區(qū)非空)。在Worker的實(shí)現(xiàn)中這個(gè)條件將按在方法setTimeout()中指定的時(shí)間周期地檢查一個(gè)鎖通知。如果在任務(wù)中不需要任何等待阻塞,僅僅只要使isCondition()方法總是返回真值。 四、任務(wù)終止時(shí)機(jī)When to terminate You'll also need the isRunning(), broadcast(), and halt() methods. Querying isRunning(), you can check whether a task is still running and make a decision whether to terminate it. The broadcast() method just notifies the lock object and makes a task proceed if it has been waiting on this lock. The halt() method stops a task, so the run() method will exit as soon as the next isRunning() status is checked. Because this method notifies only one lock that may block this task’s thread, it is advisable to use the same lock object when you do blocking operations within the work() method ( Listing B). If you can't use the same lock object, such as when you're blocking on the java.io.InputStream.read() method, you should add explicit notification of all possible locks or add java.lang.Thread.interrupt() to your halt() method. The java.lang.Thread.interrupt() works if an object you are blocked on processes this signal correctly. For example, it works for InputStream.read() but doesn’t work for java.sql.PreparedStatement.execute(), so you have to test halt() method in each particular situation. 你還需要isRunning(), broadcast(), halt()方法。通過(guò)訪(fǎng)問(wèn)isRunning()方法,你將能檢查某個(gè)任務(wù)是否正在運(yùn)行,并決定是否中止它。broadcast()方法正確地通知鎖對(duì)象,并且如果這個(gè)對(duì)象一直等待這個(gè)鎖,那么就激活這個(gè)任務(wù)。halt()方法中止一個(gè)任務(wù),因此下一isRunning()狀態(tài)一旦被調(diào)用,run()方法就退出,因?yàn)檫@個(gè)方法只通知那個(gè)可能阻塞這個(gè)任務(wù)線(xiàn)程的鎖。當(dāng)在work()方法中執(zhí)行阻塞作業(yè)時(shí)用相同的鎖是明智的。如果你不能用相同的鎖對(duì)象時(shí),例如在執(zhí)行java.io.InputStream.read()方法遇到阻塞時(shí),你就應(yīng)該添加所有可能鎖的顯式通知或者增加java.lang.Thread.interrupt()到halt()中。如果一個(gè)你阻塞的對(duì)象被正確處理,java.lang.Thread.interrupt()將會(huì)起作用。例如,它在InputStream.read()執(zhí)行時(shí)有作用,但在執(zhí)行java.sql.PreparedStatement.execute()不起作用,因此在每個(gè)特殊的條件下你必須測(cè)試halt()方法。 Once you are familiar with the Worker class, you can easily create your own implementation (Listing B). To run this class as a thread, simply use a new Thread(new WaitedWorker()).start. Applying Thread.interrupt() or Worker.halt() or a combination of them, you can control task execution precisely. For example, you can stop all workers when JVM shuts down by placing corresponding code in the java.lang.Runtime.addShutdownHook() method. 一旦你熟悉Worker類(lèi),你就很容易創(chuàng)建你自己的實(shí)現(xiàn)(參考程序清單B),為了把這類(lèi)當(dāng)作一個(gè)線(xiàn)程運(yùn)行,僅僅只需簡(jiǎn)單地使用 new Thread(new WaitedWorker()).start。應(yīng)用Thread.interrupt()或Worker.halt()或它們的組合,你就可以準(zhǔn)確的控制任務(wù)的執(zhí)行。例如當(dāng)JVM通過(guò)在java.lang.Runtime.addShutdownHook()方法中放相應(yīng)的代碼停止時(shí),你就能停止所有的任務(wù)。 四、結(jié)論Conclusion We've examined the long-running task framework and seen how to create new tasks based on its abstract class. Its architecture is clear and flexible and was designed with extensibility in mind. With this framework, you can avoid creating classes from scratch, and you'll be able to develop more efficient and reliable applications. 我們已經(jīng)檢查了長(zhǎng)時(shí)間運(yùn)行任務(wù)框架,并且看到怎樣通過(guò)從創(chuàng)建一個(gè)基于它的抽象類(lèi)的任務(wù)。它的構(gòu)架是清晰和靈活的,并且被設(shè)計(jì)成可擴(kuò)展的。用這個(gè)框架你能避免為創(chuàng)作類(lèi)而絞盡腦汁,并且?guī)椭隳軌蜷_(kāi)發(fā)出高效、可靠的應(yīng)用程序。 參考文獻(xiàn)1、 Simon Brown 2、 Joseph L.Weber 致謝非常感謝曾一起工作的同事和為JAVA技術(shù)發(fā)展做出貢獻(xiàn)的人們,沒(méi)有他們的努力成果,我就不可能感受到JAVA語(yǔ)言的魅力;同時(shí)也要感謝我的上司周洪波博士和盧盛融總工程師,他們給予我許多有益的幫助。 關(guān)于譯者胡繼鋒,軟件工程師,清華同方應(yīng)用信息系統(tǒng)本部ezONE研究院研發(fā)人員。曾經(jīng)從事DEPHI、VB、VC、C、PHP、ASP、Oracle等的開(kāi)發(fā),現(xiàn)在熱衷于JAVA技術(shù)。同時(shí)對(duì)資本市場(chǎng)有一定的興趣。

java 程序中由于數(shù)據(jù)過(guò)多導(dǎo)致時(shí)間長(zhǎng),如何優(yōu)化

如果是首頁(yè)的話(huà),對(duì)于這種高頻訪(fǎng)問(wèn)的數(shù)據(jù)用內(nèi)存緩存

用緩存可以加快加載速度,但是還是要加載的,用ajax,需要加載那里就加載那里!局部刷新比全局刷新那不是快一點(diǎn)點(diǎn)啊!頁(yè)面上的東西

能不刷新就不刷新,至于數(shù)據(jù)方面太多的話(huà)還是要做分頁(yè)的,分頁(yè)后的查詢(xún)可以每次都查詢(xún)下一頁(yè),保存到緩存,然后翻頁(yè)的時(shí)候把緩存里的拿出來(lái),后臺(tái)再去查下一頁(yè),這一體驗(yàn)效果會(huì)好很多的哦!


文章標(biāo)題:java代碼時(shí)間長(zhǎng)處理 java代碼運(yùn)行很慢怎么辦
網(wǎng)站地址:http://weahome.cn/article/ddophjs.html

其他資訊

在線(xiàn)咨詢(xún)

微信咨詢(xún)

電話(huà)咨詢(xún)

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部