Java Learning Note: Creating Threads

Source: Internet
Author: User

Multithreading
Concept: Multithreading is able to put the task in different threads simultaneously processing, can effectively improve the system's resource use, especially for multi-processor to improve the application.
Implementation concept: Put the business logic unit to be processed in a class, for the main thread is to create these classes of objects, and then each object as a thread to start, which also involves the communication of multiple threads.
Implementation method:
One by implementing the Runnable interface

1 build the class running the thread, the class needs to implement the Runnable interface, so you need to implement the run () method in the definition of the class, the content of this method body is the thread's business logic implementation code

2 Creating a Thread object
Thread (Runnable threadobj, String threadname)
Where Threadobj is the thread object created, ThreadName is the name that can be given to the thread

3 Start thread to run
Start ();

Test Cases: (the different business logic units are encapsulated in different internal classes, or can not be used as an internal class, as a common logic module for other objects to call, improve the code reuse rate.)
Package languagelearning.multithread;

public class Multithreadtest {
public class Sayhi implements Runnable {
public void Run () {
while (true) {
System.out.println ("I am Thread 1:hi");
try {
Thread.Sleep (500);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}

public class SayHello implements Runnable {
public void Run () {
while (true) {
System.out.println ("I am Thread 2:hello!");
try {
Thread.Sleep (500);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}

public static void Main (string[] args) throws Interruptedexception {
Multithreadtest t = new multithreadtest ();
Multithreadtest.sayhi hi = t.new sayhi ();
Multithreadtest.sayhello Hello = t.new SayHello ();

thread T1 = new Thread (Hi, "Thread 1");
Thread t2 = new Thread (Hello, "Thread 2");

T1.start ();
T2.start ();

while (true) {
System.out.println ("main thread");
Thread.Sleep (500);
}
}

}

Results:
[Email protected]:~/workspace/test/src$ java languagelearning.multithread.MultithreadTest
I am Thread 1:hi
Main thread
I am Thread 2:hello!
I am Thread 1:hi
Main thread
I am Thread 2:hello!
I am Thread 1:hi
Main thread
I am Thread 2:hello!
I am Thread 1:hi
Main thread
I am Thread 2:hello!
I am Thread 1:hi
...

Two by inheriting the thread class
This class can be understood by looking at the thread code in the JDK
1 Implementation steps:
Override the Run () method
It is OK to execute the start () method when calling.
Test Case:
Package languagelearning.multithread;

public class MultithreadTest1 extends Thread {
private int number;
public void Run () {
while (true) {

System.out.println ("I am thread No." + number);
try {
Thread.Sleep (1000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}

public MultithreadTest1 (int number) {
This.number = number;
}
public static void Main (string[] args) throws Interruptedexception {
Create thread
int i = 1;
while (I <= 5) {
(New MultithreadTest1 (i)). Start ();
i++;
}

Hold main thread
while (true) {
System.out.println ("I am Main thread");
Thread.Sleep (1000);
}

}

}

Results:
I am Thread No. 2
I am Main thread
I am Thread No. 5
I am Thread No. 3
I am Thread No. 4
I am Thread No. 1
I am Thread No. 2
I am Thread No. 5
I am Thread No. 3
I am Main thread
I am Thread No. 4
I am Thread No. 1
I am Thread No. 2
I am Thread No. 5
I am Main thread
I am Thread No. 4
I am Thread No. 3
I am Thread No. 1
...

Java Learning Note: Creating Threads

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.