I. Introduction to Threads and processes
For a brief introduction to processes and threads, see:
Http://blog.csdn.net/feng131719/archive/2009/06/05/4244286.aspx
two. The implementation form of the thread
Multithreading can be implemented in two ways, and it is possible to achieve multiple threads through timed execution. Here is a brief introduction to each of these implementations:
1. Inherit Thread class (Java.lang.Thread)
Create an inherited from thread class subclass. However, the thread's run () function must be overwritten to do useful work. Instead of calling this function directly, you must call the Thread's start () function, which calls run () again.
For example:
Class Primethread extends Thread {
Long Minprime;
Primethread (Long Minprime) {
This.minprime = Minprime;
}
public void Run () {
Compute primes larger than minprime
. . .
}
}
The following code then creates and starts a thread:
Primethread p = new Primethread (143);
P.start ();
When you execute the Start method, the Run method runs automatically, but one thing is done when the Start method is executed.
is to turn the thread into an executable state, and then wait for the operating system to dispatch and run, so the thread cannot be guaranteed to start immediately.
In Java, the thread class implements the Runnable interface, so the Run method is an abstract method that implements the interface runnable.
Second, direct implementation of Runnable multi-threaded interface (java.lang.Runnable)
There is only one abstract method run in the thread interface runnable that can be created by implementing a class of methods in the Runnable interface
An object that has a multithreaded feature, but the object does not enable it to start the thread, as a parameter and with the help of the thread's
Constructs a method to create an object and invoke the Start method to start the thread.
Class Primerun implements Runnable {
long minprime;
Primerun (Long minprime) {
this.minprime = minprime;
}
public void Run () {
//compute primes larger than minprime
...
}
}
The following code then creates and starts a thread:
Primerun p = new Primerun (143); New Thread (P). Start ();
three, using the task combination to achieve multithreading
In J2ME, also has the Java Task processing composition class, they are timer and timertask respectively, can use them to implement multithreading, simply say is timed to achieve the task.
A timer is a timer in Java that enables you to do something at a certain time or to do something at a certain point, by means of schedule (TimerTask Tt,long Millis) and schedule (TimerTask Tt,long Start,long off).
TimerTask is a task class that creates a task by inheriting the class and overriding the method run.
such as: public class Timertasks extends timertask{ Public Timertasks () { Constructor } public void Run () { Run code entity } } |
Task invocation:
Timer timer = new timer ();
3 seconds to perform the task
Timer.schedule (New Timertasks (), 3000);
Perform the task after 3 seconds and perform once every 5 seconds
Timer.schedule (New Timertasks (), 3000,5000);
This can be seen in the use of timing tasks to achieve the effect of threading, respectively, to perform different concurrent operations, through the Timer class object to operate the TimerTask object, through the schedule method to time the task, at the end of the task, usually using cancel () to achieve.
Usually, in the J2ME software, we trigger a series of corresponding operations through the mobile phone button, in the process of response processing more will involve network operations, data storage and other relative consumption of time and resources operations, and these operations often take a certain amount of time to complete, Therefore, in processing the key response process, we usually need to establish a threading process to avoid the program panic.
public void Commandaction (Command C, displayable s) { if (c==do1com) { ///Create Implementation interface thread new Thread (new Runnablesimple ()). Start (); } Else if (c==do2com) { //Create Inherits thread thread new Threadsimple (). Start (); else{ //Create task Threads new Timer (). Schedule (New Timertasks (), 3000,20); } } |