Multi-threaded programming-Linux general technology-Linux programming and kernel information. The following is a detailed description. ◆ Introduction to multithreading
A thread is the basic entity for the operating system to allocate CPU time. Each application has at least one thread and multiple threads. A thread is a code stream in a program. Multiple Threads can run simultaneously and share resources.
Different from processes, each process requires the operating system to allocate an independent address space for it. The threads in the same process work in the same address space.
In Java programs, dynamic effects (such as animation implementation and dynamic subtitles) are often implemented using multithreading.
Multithreading can be easily used in Java. Compared with Visual C ++, Java multithreading is easier to learn and use.
◆ Two methods for creating multithreading:
(1) create a sub-class of Thread
(2) Implement interface Runnable
The second method is more widely used than the first one. This lecture focuses on the second approach.
◆ Interface
Java removes the multi-inheritance of c ++ ("multi-inheritance" often confusions C ++ programmers ). Java has added the "interface" concept, so that Java does not degrade its functionality after removing multiple inheritance.
An interface is a special class. When defining a class, you can "implement" one or more interfaces. Syntax:
Class name extends superclass name implements Interface Name
◆ Example 1.5.1 a simple multi-threaded small Application
Import java. applet .*;
Import java. awt .*;
Public class k04a extends Applet implements Runnable
{
Private Thread m_k04a = null;
Public k04a ()
{
}
Public void paint (Graphics g)
{
G. drawString ("Running:" + Math. random (), 10, 20 );
}
Public void start ()
{
M_k04a = new Thread (this );
M_k04a.start ();
}
Public void stop ()
{
M_k04a.stop ();
M_k04a = null;
}
Public void run ()
{
While (true)
{
Try
{
Repaint ();
Thread. sleep (200 );
}
Catch (InterruptedException e)
{
Stop ();
}
}
}
}
◆ Control the thread Lifecycle
(1) start () method to start a thread
(2) The run () method defines the action of the thread.
(3) sleep () method enables the thread to sleep for a period of time, in milliseconds
(4) The suspend () method suspends the thread
(5) resume the suspended thread using the resume () method.
(6) The yield () method moves the thread to the end of the queue
(7) stop () method ends the thread life cycle and performs cleanup
(8) The destroy () method ends the thread lifecycle without cleaning.
The most common ones are start (), run (), sleep (), and stop ().
◆ Try ?? Catch statement
It is used to handle "exceptions. Compared with "error", "exception" is mild. It refers to exceptions that occur during the running of the program. (Try-catch statements are also available in C ++ ).
If an exception occurs when executing the statement after try, the catch statement is executed.
◆ Example 1.5.2 stream title
When the program is running, the three titles are constantly changed from bottom to bottom.
Statement in HTML file: (three images must be prepared in advance)
JAVA source code:
Import java. awt .*;
Import java. applet. Applet;
Public class testani extends Applet implements Runnable
{
Thread runner;
Image imgs [];
Int high, y1, y2, y3;
Public void init ()
{
High = size (). height;
Y1 = high;
Y2 = high * 2;
Y3 = high * 3;
Imgs = new Image [10];
For (int I = 0; I <3; I ++)
Imgs = GetImage (getCodeBase (), getParameter ("image" + (I + 1 )));
}
Public void start ()
{
Runner = new Thread (this );
Runner. start ();
}
Public void stop ()
{
Runner. stop ();
Runner = null;
}
Public void run ()
{
While (runner! = Null)
{
Try
{
Thread. sleep (100 );
Repaint ();
Y1 --;
If (y1 = 0)
{
Thread. sleep (3000 );
Y2 = high;
}
Y2 --;
If (y2 = 0)
{
Thread. sleep (3000 );
Y3 = high;
}
Y3 --;
If (y3 = 0)
{
Thread. sleep (3000 );
Y1 = high;
}
}
Catch (InterruptedException e ){}
}
}
Public void paint (Graphics g)
{
G. drawImage (imgs [0], 0, y1, this );
G. drawImage (imgs [1], 0, y2, this );
G. drawImage (imgs [2], 0, y3, this );
}
Public void update (Graphics g)
{
Paint (g );
}
}
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