And the thread pool thread on the basic knowledge

  ?? Thread is a major characteristic of Java, it can be given the instruction sequences, the method given in the definition of variables or some sharing of data (a type of variable).    Each thread in Java have their own stack and the program counter (PC), which is used to track thread stack in the context of (the context is that when the implementation of the thread somewhere, the current value of local variables), and procedures Counters are used to track the current thread is the implementation of the directive. 

  î—¥ î—¥ under normal circumstances, a thread can not visit another thread stack variables, and the threads must be in a state of the following: 

  î—¥ î—¥ 1. Queue status (Ready), the user creates a thread, the thread that will run immediately.    When the method of thread start () is called, the thread will be a state of the queue, waiting for the scheduling procedures will be running into it (Running).    When a process is executed after it can be queuing state.    If scheduling procedures permitting, by calling the methods yield () would be in the process Add to queue status. 

  î—¥ î—¥ 2. Running (Running), when scheduling procedures CPU running time allocated to a thread, the thread entered the running started operation. 

  î—¥ î—¥ 3. Wait for the state (Waiting), many reasons can lead to threads in a wait state, such as in the implementation of the thread was suspended, or wait for the I / O requests into waiting for the completion of the state. 

  î—¥ î—¥ in Java different threads with different priorities, high-priority threads can be arranged in low-priority threads to complete.    If multiple threads have the same priority, Java will switch between different threads running.    An application can be threaded through the use of the method setPriority () to set the thread priority, using getPriority () to obtain the priority of a thread. 

  î—¥ î—¥ threads of the life cycle 

  î—¥ î—¥ a thread of the life cycle can be divided into two phases: survival (Alive) cycle and death (Dead) cycle, which also include life-cycle operation of state (Running), and wait for the state (Waiting).    When creating a new thread, the thread queuing to enter the state (Ready), when the method of thread start () is called, the thread into the life-cycle, when it means isAlive () always return true value until the thread into the death of state. 

  î—¥ î—¥ the realization of thread 

  î—¥ î—¥ There are two ways to achieve thread, and the other is to expand java.lang.Thread class, and the other is through the java.lang.Runnable interface. 

  î—¥ î—¥ Thread class encapsulates thread acts.    To create a thread, we must create a category expanded from Thread of new categories.    As the Thread class methods run () does not provide any operation, therefore, in the creation of threads, users must cover methods run () to complete useful work.    When the method of thread start () is called, and methods run () to be called.    The following code is through expansion to achieve thread Thread type: 

  Import java.awt .*; 
  (Class Sample1 
  Public static void main (String [] args) ( 
  ? Mythread test1 = new Mythread (1); 
  ? Mythread test2 = new Mythread (2); 
  ? Test1.start (); 
  ? Test2.start (); 
  ) 
  ) 
  Class Mythread? Extends Thread ( 
  Int id; 
  Mythread (int i) 
  (Id = i;) 
  Public void run () ( 
  ? Int i = 0; 
  ? While (id + i == 1) ( 
  ? Try (sleep (1000); 
  ?) Catch (InterruptedException e) () 
  ) 
  System.out.println ( "The id? Is" + id); 
  ) 

  î—¥ î—¥ usually when the user wants a class will be able to run in their own thread, but also extended certain other categories of characteristics, it is necessary to run through the Runnable interface to achieve.    Runnable interface method only one run ().    No matter what the time created a use of the Runnable interface type, must be prepared in the class run () method in the interface to cover the run () method.    For example, the following code is through Runnable interface thread: 

  Import java.awt .*; 
  Import java.applet.Applet; 
  Public class Bounce extends Applet implements Runnable ( 
  Static int r = 30; 
  Static int x = 100; 
  Static int y = 30; 
  Thread t;? 
  Public void init () 
  ( 
  ? T = new Thread (this); 
  ? T.start (); 
  ) 
  Public void run () 
  ( 
  ? Int y1 = +1;? 
  ? Int i = 1; 
  ? Int sleeptime = 10; 
  ? While (true) 
  ? ( 
  ? Y + = (i * y );???? 
  ? If (yr <i ||y+r> getSize (). Height )????????????? 
  ? Y1 *=- 1; 
  ? Try ( 
  ? T.sleep (sleeptime); 
  ?) Catch (InterruptedException e) () 
  ?) 
  ) 
  ) 

  î—¥ î—¥ Why you should use a thread pool 

  î—¥ î—¥ in Java, if every time a request arrived on the creation of a new thread, the cost is considerable.    In actual use, every request to create a new thread in the server thread creation and destruction, and time spent on the consumption of system resources, and may even than to dealing with the actual users of the request is much more time and resources.    In addition to creating and destroying threads overhead, the thread also need to consume system resources.    If a JVM in the creation of too many threads, may lead to excessive consumption due to system memory or "switch over" and lead to lack of system resources.    In order to prevent the lack of resources, application servers need some way to limit any given moment with the number of requests, minimize the creation and destruction of the number of threads, in particular some of the resources spent relatively large thread creation and destruction, to make full use of has been targeted for this service is the "pool of resources" from technical reasons. 

  î—¥ î—¥ main thread pool thread to solve the problem life cycle costs and inadequate resources issues.    Through the multiple tasks reusable threads, thread creation costs to be assessed on a number of tasks, but also due to the arrival of threads in the request already exists, so the threads created by the elimination of the delay.    Therefore, we can immediately request services to enable faster application response.    In addition, through the appropriate adjustments to the number of thread pool thread can prevent the shortage of resources. 

  î—¥ î—¥ create a thread pool 

  î—¥ î—¥ a relatively simple thread pool shall contain at least the thread pool management, thread work, job queue, the task of the interface.    One thread pool management (ThreadPool Manager) is the role of creation, destruction and manage the thread pool will work Add thread pool thread; work can be recycled is a thread implementation of the mandate of threads, in the absence of mandate to wait; job queue role is to provide a buffer mechanism would not deal with the tasks on the job queue; task interface is the need for each task interface, the main entrance to the mandate, the mandate after The relevant work, such as implementation of the mandate of the state, the thread through the implementation of the mandate of scheduling interface.    The following code to achieve the creation of a thread pool, as well as removed from the thread pool thread operation: 

  Public class ThreadPool 
  (? 
  Private Stack threadpool = new Stack (); 
  Private int poolSize; 
  Private int currSize = 0; 
  Public void setSize (int n) 
  ( 
  ? PoolSize = n; 
  ) 
  Public void run () 
  ( 
  ? For (int i = 0; i <poolSize; i + +) 
  ? ( 
  ? WorkThread workthread = new WorkThread (); 
  ? Threadpool.push (workthread); 
  ? CurrSize + +; 
  ?) 
  ) 
  Public? Synchronized WorkThread? Getworker () 
  ( 
  ? If (threadpool.empty ()) 
  ? System.out.println ( "stack is empty"); 
  ? Else 
  ? Try (return threadpool.pop (); 
  ?) Catch (EmptyStackException e) () 
  ) 
  ) 

  î—¥ î—¥ thread pool for application of occasions 

  î—¥ î—¥ when a Web server received a large number of requests for short-thread, the use of the thread pool is a very appropriate technology, which can greatly reduce the creation and destruction of thread number, improve server efficiency.    But if the requirements of the threads running relatively long time, then the thread running time is much longer than the time to create, to reduce creation time alone on the efficiency of the system not obvious, is not suitable for this application thread pool technology, the need With other technologies to improve the efficiency of services server.    Published in 2007-01-31 10:29 stme reading (538) Comments (0) edit their collections quoted Category: Algorithm 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply