J2ME multi-thread (entry level)

xiaoxiao2021-03-06  19

Threads are also called light processes (LWP). Because threads can only act within a single process, create a thread is cheap than the creation process.

Much more. Thus, because the thread allows collaboration and data exchange, it is very cheap in computing resources, thread is more desirable than the process. Thread needs

To operate the system, it is not all the machines to provide a thread. Java programming language, as a quite new language, threaded

It is also integrated with the language itself, and J2ME as a small version of Java provides strong support for threads. In Java language programming,

There are only two ways to use threads: First, by inheriting the Thread class, the second is to implement the Runnable interface in the class of the thread.

The Thread THREAD class is a specific class, ie not an abstract class, which encapsulates the behavior of threads. To create a thread, you must create an inherited from the Thread class. The programmer must override the thread's Run () function to complete useful work. This function is not called directly; but the Start () function of Thread () must be called, the function calls Run ().

The following code illustrates its use: import javax.microedition.lcdui *; import javax.microedition.midlet *; public class Bounce extends MIDlet {private BounceCanvas myCanvas; / ** * declaration RectThread thread class * / private RectThread rectThreads.. []; private boolean done = false; private Display myDisplay; / ** * repaint lock * / private Object repaintLock = new Object (); Bounce () {myDisplay = Display.getDisplay (this); myCanvas = new BounceCanvas (); CreateRectThreads ();} / * * Create four rectangular box * / void createrectthreads () {RectThreads = new RectThread [4]; RectThreads [0] = New RectTHread (2, 2, 30, 30, 8, mycan); RectThreads [1] = New RectTHread (-2, -2, 40, 40, 4, mycanvas); RectThreads [2] = New RectTHread (-1, -1, 50, 10, 10, mycanvas); RectThreads [3] = new RectThread (4, 4, 10, 10, 2, myCanvas);} / ** * start thread * / protected void startApp () throws MIDletStateChangeException {myDisplay.setCurrent (myCanvas); if (rectThreads == null) {createRectThreads ( );} for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-41149.html

New Post(0)