What is a thread
A thread refers to an execution process in a process. Multiple processes can run in one process. thread. For example, many threads can run in the java.exe process. Threads always belong to a process, and multiple threads in a process share the memory of the process.
In Java, "thread" refers to two different things:
1. An instance of the java.lang.Thread class;
2. The execution of a thread.
Recommended java related video tutorials: java video tutorial
Use the java.lang.Thread
class or java.lang.Runnable
Interface Write code to define, instantiate and start new threads.
A Thread class instance is just an object, like any other object in Java, has variables and methods, lives and dies on the heap.
In Java, each thread has a call stack. Even if no new threads are created in the program, the thread is still running in the background.
A Java application always starts running from the main()
method. The main() method runs in a thread, which is called the main thread.
Once a new thread is created, a new call stack is generated.
Threads are generally divided into two categories: user threads and waiting threads.
When all user threads finish executing, the JVM automatically shuts down. However, the waiting thread is not independent of the JVM. The waiting thread is generally created by the operating system or the user.
Java thread: creation and startup
1. Define thread
1. Extend java .lang.Thread class.
There is a run() method in this class, and you should pay attention to its usage:
public void run()
If the thread is constructed using an independent Runnable running object, call the run method of the Runnable object; Otherwise, the method does nothing and returns.
Subclasses of Thread should override this method.
2. Implement the java.lang.Runnable interface.
void run()
When you create a thread using an object that implements the interface Runnable, starting the thread will cause the object's run method to be called in an independently executed thread.
The general contract of the method run is that it may perform any desired operation.
2. Instantiate threads
1. If it is a thread that extends the java.lang.Thread class, just use new.
2. If it is a class that implements the java.lang.Runnable interface, use the Thread constructor:
Thread(Runnable target) Thread(Runnable target, String name) Thread(ThreadGroup group, Runnable target) Thread(ThreadGroup group, Runnable target, String name) Thread(ThreadGroup group, Runnable target, String name, long stackSize)
3. Start the thread
Call the start() method on the thread's Thread object instead of run() or other methods.
Before calling the start() method: the thread is in a new state. The new state means that there is a Thread object, but there is not yet a real thread.
After calling the start() method: a series of complex things happened
Start a new execution thread (with a new call stack);
The thread starts from the new state Transfer to a runnable state;
When the thread gets a chance to execute, its target run() method will run.
Note: There is nothing special about the run() method for Java. Like the main() method, it's just the method name (and signature) that the new thread knows to call. Therefore, it is legal to call the run method on Runnable or Thread. But it does not start a new thread.
Recommended related articles and tutorials: Zero Basic Introduction to Java
The above is the detailed content of What is a thread in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

DependencyInjection(DI)isadesignpatternwhereobjectsreceivedependenciesexternally,promotingloosecouplingandeasiertestingthroughconstructor,setter,orfieldinjection.2.SpringFrameworkusesannotationslike@Component,@Service,and@AutowiredwithJava-basedconfi

TheJVMenablesJava’s"writeonce,runanywhere"capabilitybyexecutingbytecodethroughfourmaincomponents:1.TheClassLoaderSubsystemloads,links,andinitializes.classfilesusingbootstrap,extension,andapplicationclassloaders,ensuringsecureandlazyclassloa

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear

ChromecanopenlocalfileslikeHTMLandPDFsbyusing"Openfile"ordraggingthemintothebrowser;ensuretheaddressstartswithfile:///;2.SecurityrestrictionsblockAJAX,localStorage,andcross-folderaccessonfile://;usealocalserverlikepython-mhttp.server8000tor

Networkportsandfirewallsworktogethertoenablecommunicationwhileensuringsecurity.1.Networkportsarevirtualendpointsnumbered0–65535,withwell-knownportslike80(HTTP),443(HTTPS),22(SSH),and25(SMTP)identifyingspecificservices.2.PortsoperateoverTCP(reliable,c

Pre-formanceTartuptimeMoryusage, Quarkusandmicronautleadduetocompile-Timeprocessingandgraalvsupport, Withquarkusoftenperforminglightbetterine ServerLess scenarios.2.Thyvelopecosyste,
