In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What are the ways to create multithreading in Java? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
The difference between threads and processes
* * process: * * A running program, such as the QQ Music .exe program you open, which consists of PCB (process control block), stack, program and data.
Thread: an execution sequence of program code in a process, which is considered to be the encapsulation of a virtual processor with its own program code and data, which consists of three parts: processor, code and data. you can understand that threads are doing things within the process at the same time, for example, you can listen to music and download music at the same time in QQ Music app. This is achieved by multithreading. (a process contains at least one or more threads)
Note:
Every Java program has an implicit main thread, the main () method. To implement multithreading, a new thread must be created in the main thread. The Java language uses objects of the Thread class and its subclasses to represent the thread.
A virtual processor is encapsulated in the java.lang.Thread class, which controls the operation of the whole thread; the code executed by CPU is passed to the Thread class, and the Thread class controls the sequential execution; the processed data is passed to the Thread class, which is the data to be processed in the process of code execution. Code and data can be shared by multiple threads or not. Code and data are independent of each other. When two threads share an instance of the same class, they share the same code. When two threads share access to a common object, they share the same data.
Life cycle of a thread
A thread goes through a complete life cycle from its creation to stopping execution, in which there are four different states.
① New (new) ② Runnable (runnable) ③ Blocked (blocked) ④ Dead (dead)
There are three ways to create a thread by inheriting the Thread class package thread;public class ThreadTest extends Thread {String threadName; public ThreadTest1 (String s) {System.out.println ("Making thread:" + s); threadName = s;} public void run () {for (int I = 0; I)
< 3; i++ ){ System.out.println("Running thread number =" + threadName); try { Thread.sleep(1000); //线程睡眠1秒 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //主线程,操作系统调度每个线程执行 public static void main(String[] args){ ThreadTest1 thread1 = new ThreadTest1("Tom"); //线程1Tom ThreadTest1 thread2 = new ThreadTest1("Jack"); //线程2Jack /**线程启动,不是直接调用 run() 方法,而是调运线程类 Thread 的 start() 方法, *在 Thread 方法内部会调用本地系统方法,最终会自动调用自己线程类的 run()方法。 **/ thread1.start(); thread2.start(); System.out.println("End of main"); }} 运行截图 通过实现Runnable接口package thread;public class ThreadTest implements Runnable{ String threadName; public ThreadTest(String s){ System.out.println("Making thread:"+s); threadName = s; } public void run(){ for( int i = 0; i < 3; i++ ){ System.out.println("Running thread number =" + threadName); try { Thread.sleep(1000); //让线程睡眠1秒 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //主线程,操作系统调度每个线程执行 public static void main(String[] args){ //创建线程Tom和线程Jack Thread thread1 = new Thread(new ThreadTest("Tom")); Thread thread2 = new Thread(new ThreadTest("Jack")); /**线程启动,不是直接调用 run() 方法,而是调运线程类 Thread 的 start() 方法, *在 Thread 方法内部会调用本地系统方法,最终会自动调用自己线程类的 run()方法。 **/ thread1.start(); thread2.start(); System.out.println("End of main"); }} 运行截图 通过实现Callable接口package thread;import java.util.concurrent.Callable;import java.util.concurrent.FutureTask;public class ThreadTest implements Callable{ String threadName; public ThreadTest(String s) { System.out.println("Making thread:"+s); threadName = s; } @Override public Integer call() throws Exception { int sum = 0; // TODO Auto-generated method stub for( int i = 0; i < 5; i++ ){ System.out.println(threadName+i); sum += i; } return sum; } public static void main(String args[]){ //创建两个线程,并且启动 ThreadTest threadTest1 = new ThreadTest("Tom"); ThreadTest threadTest2 = new ThreadTest("Jack"); FutureTask result1 = new FutureTask(threadTest1); new Thread(result1).start(); FutureTask result2 = new FutureTask(threadTest2); new Thread(result2).start(); }} 运行截图 线程类型分类 java中线程一共有两种类型,守护线程和用户线程,用户线程又叫非守护线程。 守护线程 可以通过thread.setDaemon(true)方法设置线程是否为守护线程,thread.setDaemon(true)必须在thread.start()之前设置,否则会抛出IllegalThreadStateException异常。在守护线程中开启的新线程也是守护线程。守护线程顾名思义是用来守护的,是给所有的非守护线程提供服务的,所以在JVM执行完所有的非守护线程后,JVM就会停止,守护线程也不在运行,最典型的守护线程就是java的垃圾回收机制(GC)。 非守护线程 java线程默认设置为非守护线程thread.setDaemon(false)。当主线程运行完之后,只要主线程里有非守护线程JVM就互惠退出,直到所有的非守护线程执行完之后JVM才会退出。、 >Summary: if a thread is set to be a daemon thread, the exit of the JVM will not care about the execution state of the current thread.
After reading the above, have you mastered the ways to create multithreading in Java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.