Continue to learn-----------------------------------------------------------------------
In the Java thread refers to two things
1) An example of the Java.lang.Thread class
2) Execution of threads
A thread class instance is just an object that lives and dies on the heap. The Main method is a main thread
Java Thread creation and startup:
1) Extended Java.lang.Thread class
2) Implement Java.lang.Runnable interface
Instantiating Threads:
1) If you are extending the thread, simply use new to
2) If you are a class that implements the Runnable interface, use the thread's construction method
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)
To start a thread:
Call the Start method
Start a new execution thread
Running the Run method
Instance code:
Package Com.ehealth.thread;public class Runnabledemo implements Runnable{private String name;public Runnabledemo ( String name) {this.name=name;} @Overridepublic void Run () {for (int. i=0;i<5;i++) {for (int k=0;k<5;k++) System.out.println (name+ ":" +i);}}}
Package Com.ehealth.thread;public class Testthread extends Thread{public testthread (String name) {super (name);} public void Run () {for (int. i=0;i<5;i++) {for (int k=0;k<5;k++) {System.out.println (This.getname () + ":" +i);}}} public static void Main (string[] args) {Thread t1=new testthread ("JOHNW"); Thread t2=new testthread ("wee"); T1.start (); T2.start ();}}
Own understanding of:
If the Runnable interface is implemented, the Run method must be implemented. When you start a thread, you must first new itself, the thread object in new, and pass yourself as a parameter to thread.
If you inherit the thread class, rewrite the Run method again, and then new will be able to start
The Run method contains the execution logic of the thread that you want to implement.
It feels like too many threads. Here is the first excerpt and summarize the basis of it. Finally, you can learn to combine sockets with threads and IO streams.
This article from "ehealth" blog, declined reprint!
Java Learning notes----threads