Java: Threads Everywhere

Source: Internet
Author: User
Tags event listener thread

Who created the thread?

Even if you have never explicitly created a new thread, you may still find yourself using the thread. Threads are introduced into our programs from a variety of sources.

There are many tools that can create threads for you, and if you want to use these tools, you should understand how threads interact and how to prevent threads from interfering with one another.

AWT and Swing

Any program that uses AWT or Swing must handle threads. The AWT Toolbox creates a single thread for handling UI events, and any event listeners invoked by the AWT event are executed in the AWT event thread.

You must not only care about synchronizing access to data items that are shared between event listeners and other threads. You must also find a way to run a long-running task that is triggered by an event listener, such as checking spelling in a large document or searching for a file in a file system, running in a background thread, so that when the task runs, the UI Will not stagnate (this may also prevent the user from canceling the operation). A good framework example for doing this is the SwingWorker class (see Resources).

The AWT event thread is not a daemon thread; This is why you typically use System.exit () to end AWT and Swing applications.

Using TimerTask

In JDK 1.3, the TimerTask tool is introduced into the Java language. This handy tool allows you to perform a task at a later time (for example, to run a task 10 seconds from now), or to perform a task on a regular basis (that is, to run the task every 10 seconds).

Implementing the Timer class is simple: it creates a timer thread and constructs a wait event queue that is sorted by execution time.

The TimerTask thread is marked as a daemon thread, so that it does not prevent the program from exiting.

Because the timer event is performed in a timer thread, you must ensure that access to any data items used in the timer task is correctly synchronized.

In the calculateprimes example, there is no main thread to hibernate, and we can use TimerTask as follows:

public static void main(String[] args) {
    Timer timer = new Timer();
    
    final CalculatePrimes calculator = new CalculatePrimes();
    calculator.start();
    timer.schedule(
        new TimerTask() {
          public void run()
          {
            calculator.finished = true;
          }
        }, TEN_SECONDS);
  }

Servlet and JavaServer Pages technology

The servlet container creates multiple threads that execute servlet requests in these threads. As a servlet writer, you do not know (and should not know) what thread your request executes on, and if multiple requests to the same URL are inbound, the same servlet may be active in multiple threads at the same time.

When you write a servlet or JavaServer Pages (JSP) file, you must always assume that you can execute the same servlet or JSP file concurrently in multiple threads. You must properly synchronize any shared data that is accessed by the servlet or JSP file, including the fields of the Servlet object itself.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.