processes and threads (thread) are the two basic units that a program executes.
Java concurrent programming Many of the others are thread-related.
Process
A process is an independent execution unit that can be thought of as a program or an application. However, a colleague within a program also includes multiple processes.
The Java Execution time environment is a separate process, and within it are various classes and programs that are used as processes.
Thread
The ability to consider threads as lightweight processes.
Threads exist in the process and require less resource overhead. The threads in the same process share the resources of the process.
Java multithreading
Each Java reference has to have only one thread-the main threads (main thread). While there are more threads executing in the background, such as memory management, system management, signal processing, and so on, from the application's point of view, Main is the first thread, and we are able to create multiple threads from it.
Advantages of threading
1. The thread is lightweight when compared to the process. The time overhead and resource overhead for creating threads are very small.
2. Threads of the same process share data and code for the process.
3. The overhead of context switching between threads is typically less than the process.
4. Inter-thread communication is more convenient than interprocess communication.
In programming. Java provides two ways to create threads:
1. Implement the Java.lang.Runnable interface
2. Inheriting the Java.lang.Thread class
Java Threading Demo Sample-Implement Runnable interface
In order for the class to execute, we need to implement the Java.lang.Runnable interface and provide the implementation in the public Void Run () method. At the same time. It is also necessary to create a thread object and break into the class that implements the Runnable interface, so that the ability to call start () executes run ()in a separate thread.
The following is a Java class that implements the Runnable interface.
Heavyworkrunnable.java
Package Com.journaldev.threads;public class Heavyworkrunnable implements Runnable { @Override public void Run ( { System.out.println ("Doing heavy Processing-start" + thread.currentthread (). GetName ()); try { thread.sleep (+); Get database connection, delete unused data from DB dodbprocessing (); } catch (Interruptedexception e) { E. Printstacktrace (); } System.out.println ("Doing heavy Processing-end" + thread.currentthread (). GetName ()); } private void Dodbprocessing () throws interruptedexception { thread.sleep; }}
Java Threading Demo sample-inheriting the thread class
The ability to create your own thread classes by integrating the Java.lang.Thread class and overriding the run () method. We were able to create the object of the thread class and call the start () method to run the well-defined running method.
The following example shows how to integrate the thread class.
Mythread.java
Package Com.journaldev.threads;public class MyThread extends Thread {public MyThread (String name) { Super (name ); } @Override public Void Run () { System.out.println ("Doing heavy Processing-start" + thread.currentthread (). GetName ()); try { thread.sleep (+); Get database connection, delete unused data from DB dodbprocessing (); } catch (Interruptedexception e) { E.printstacktrace (); } System.out.println ("Doing heavy Processing-end" + thread.currentthread (). GetName ()); } private void Dodbprocessing () throws interruptedexception { thread.sleep; }}
The following test program demonstrates how to create and run a thread.
Threadrunexample.java
Package Com.journaldev.threads;public class Threadrunexample {public static void Main (string[] args) { Thread T1 = new Thread (new Heavyworkrunnable (), "T1"); Thread t2 = new Thread (new Heavyworkrunnable (), "T2"); System.out.println ("Starting Runnable threads"); T1.start (); T2.start (); System.out.println ("Runnable Threads has been started"); Thread t3 = new MyThread ("T3"); Thread T4 = new MyThread ("T4"); System.out.println ("Starting mythreads"); T3.start (); T4.start (); System.out.println ("Mythreads has been started"): }}
The above Java program output results such as the following:
Starting Runnable threadsrunnable Threads has been starteddoing heavy Processing-start t1doing heavy Processing-start T2starting Mythreadsmythread-start Thread-0mythreads has been startedmythread-start thread-1doing heavy processing-e ND t2mythread-end thread-1mythread-end thread-0doing heavy processing-end t1
Once we start the thread, it depends on the time shard of the operating system. We cannot control the running of the thread.
However, we are able to prioritize threads, but there is no guarantee that high-priority threads will run first.
Compare runnable and Thread
Assuming your class is not just executed as a thread, but rather requires a lot of other functionality, you should implement the Runnable interface. Assume that your class's goal is simply to execute as a thread. You can inherit the thread class directly.
The implementation of the Runnable interface is better than inheriting the thread class, since Java supports multi-interface implementations. Once your class inherits the thread class. Then it can no longer inherit other classes.
tip: you may have noticed. The thread does not return whatever value. But let's say we want the thread to return processing results to the client after the process has finished working. Can participate in the article "Java callable Future".
Update: Starting with Java 8, the runnable agenda is a functional interface of sincerity. Instead of using an anonymous class, we can use a lambda expression to implement it. See "Java 8 Lambda Expressions Tutorial" for details.
Original address: Java thread example-extending thread Class and implementing Runnable Interface
Java Threading Demo sample-inheriting the thread class and implementing the Runnable interface