In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what are the basic knowledge points of Java multithreading", which is easy to understand and organized. I hope it can help you solve your doubts. Let me lead you to study and learn what are the basic knowledge points of Java multithreading.
I. Thread
What is a thread:
A thread is an entity of a process and the basic unit of CPU scheduling and dispatching. It is a smaller basic unit that can run independently than a process.
What is multithreading:
Multithreading means that multiple different threads can be run simultaneously in a single program to perform different tasks.
Second, the way to create multithreading
There are three ways to create multithreading: Thread, Runnable, and Callable
1. Inherit the Thread class to implement multithreading
Thread thread = new Thread () {@ Override public void run () {super.run (); while (true) {try {Thread.sleep (500);} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("1:" + Thread.currentThread () .getName ()); System.out.println ("2:" + this.getName ());}; thread.start () 2. Implement multithreaded Thread thread1 = new Thread (new Runnable () {@ Override public void run () {while (true) {try {Thread.sleep (500);} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("3:" + Thread.currentThread (). GetName ()); thread1.start (); 3. Callable API creation thread public class CallableTest {public static void main (String [] args) {System.out.println ("current thread is:" + Thread.currentThread ()) Callable myCallable = new Callable () {@ Override public Integer call () throws Exception {int I = 0; for (; I < 10) Current thread System.out.println ("current thread is:" + Thread.currentThread () + ":" + I); return I;}}; FutureTask fu = new FutureTask (myCallable); Thread th = new Thread (fu, "callable thread") Th.start (); / / get the return value try {System.out.println ("the return value is:" + fu.get ());} catch (Exception e) {e.printStackTrace ();}
The current thread is: Thread [main,5,main]
The current thread is: Thread [callable thread,5,main]: 10
The return value is: 10
Summary:
Implementing the Runnable interface has the following advantages over inheriting the Thread class:
The limitations caused by the single inheritance feature of Java can be avoided.
Enhance the robustness of the program, the code can be shared by multiple threads, and the code and data are independent.
It is suitable for multiple thread areas of the same program code to deal with the same resource.
The difference between implementing the Runnable interface and implementing the Callable interface:
Runnable has been available since java1.1, while Callable has been added since 1.5.
The method specified by Callable is call (), and the method specified by Runnable is run ()
Callable's task can return a value after execution, while Runnable's task cannot return a value of (void)
The call method can throw an exception, but the run method cannot
Run the Callable task to get a Future object that represents the result of the asynchronous calculation. It provides a way to check whether the calculation is complete, to wait for the calculation to complete, and to retrieve the results of the calculation. Through the Future object, you can know the execution of the task, cancel the execution of the task, and obtain the execution result.
Join the thread pool to run, Runnable uses the execute method of ExecutorService and Callable uses the submit method.
Third, the life cycle and state of threads
Fourth, the execution order of threads
The running order of Join threads
Principle:
1. Timer import java.util.Timer;import java.util.TimerTask;public class TraditionalTimerTest {public static void main (String [] args) {/ / new Timer () .schedule (new TimerTask () {/ @ Override// public void run () {/ System.out.println ("bombing!"); / /} /}, 10000) Class MyTimberTask extends TimerTask {@ Override public void run () {System.out.println ("bombing!"); new Timer () .schedule (new MyTimberTask (), 2000);}} new Timer () .schedule (new MyTimberTask (), 2000); int count = 0 While (true) {System.out.println (count++); try {Thread.sleep (1000);} catch (InterruptedException e) {e.printStackTrace ();}
0
one
Bombing!
two
three
Bombing!
four
five
Bombing!
six
Omit.
2. Mutual exclusion and synchronous communication of threads public class TraditionalThreadSynchronized {public static void main (String [] args) {new TraditionalThreadSynchronized (). Init ();} private void init () {final Outputer outputer = new Outputer () New Thread (new Runnable () {@ Override public void run () {while (true) {try {Thread.sleep (10);} catch (InterruptedException e) {e.printStackTrace () } outputer.output ("kpioneer");}) .start () / / new Thread (new Runnable () {/ / @ Override// public void run () {/ while (true) {/ / try {/ / Thread.sleep (10) / /} catch (InterruptedException e) {/ / e.printStackTrace (); / /} / / outputer.output2 ("Tom"). Start () New Thread (new Runnable () {@ Override public void run () {while (true) {try {Thread.sleep (10);} catch (InterruptedException e) {e.printStackTrace () } outputer.output3 ("Jack");}. Start ();} static class Outputer {public void output (String name) {int len = name.length (); synchronized (Outputer.class) {for (int I = 0; I < len) ) {System.out.print (name.charAt (I));} System.out.println ();}} public synchronized void output2 (String name) {int len = name.length (); for (int I = 0; I < len) System.out.println +) {System.out.print (name.charAt (I));} System.out.println ();} public static synchronized void output3 (String name) {int len = name.length (); for (int I = 0; I < len; I +) {System.out.print (name.charAt (I)) } System.out.println ();} 3. Thread synchronous communication technology
Child thread loop 10 times, then the main thread loop 100, then return to the child thread loop 10 times, then return to the main thread loop 100, so loop 50 times, please write the program.
Public class TraditionalThreadCommunication {public static void main (String [] args) {final Business business = new Business (); new Thread (new Runnable () {@ Override public void run () {for (int I = 1; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.