Objective
A thread is a sequential flow of control within a program. The CPU actually executes one at a point in time. It's just that we split the cup into multiple time slices, and because it's very fast, we look like multiple threads. Just like your time is divided into several pieces, so the overall seems to do things regularly, efficiency will be high, not to mention is the cup.
To create and start a thread:
(1) Defines a subclass of a Thread
class and overrides the run()
method of the class, which run()
represents the task the thread needs to complete. So the run()
method is called the thread execution body
(2) Creating Thread
An instance of a subclass, that is, creating a thread object
(3) Invoke the thread object's start()
method to start the thread
Source:
Create thread class public
class Firstthread extends thread
{
private int i
by inheriting the thread class; Overriding the Run () method, the method body of the run () method is the thread execution body public
void run ()
{for
(; i<100;i++)
{//When the thread class inherits the Threads class. Use this directly to get
the GetName () of the current data//thread object to return the name of the current thread
//So you can call the GetName () method directly to return the name of the current thread
System.out.println (GetName () + "" +i);
}
public static void Main (string[] args)
{
for (int i=0;i<100;i++)
{
//Call thread's CurrentThread () Method gets the current thread
System.out.println (Thread.CurrentThread (). GetName () + "" +i);
if (i==20)
{
//Create and start the first thread
new Firstthread (). Start ();
Create and start the second thread
new Firstthread (). Start ();
Run the interface:
Summarize
The above is the entire content of this article, I hope that the study and work to help. If you have questions, you can leave a message for discussion.