The state of the thread:
1. Threads also have a fixed state of operation:
Create state: A Multithreaded object is prepared (that is, new comes out with a thread object )
Ready state: Call the Start () method , waiting for the CPU to dispatch (waiting for the CPU to allocate the state of the resource)
Run state: executes the Run () method (when the CPU is allocated a resource or the CPU has been dispatched, it is running)
Blocking status: temporarily stops execution and may give resources to other threads for use (can be restored to a running state)
Terminating state (dead State): Thread destruction (eventually the thread is automatically destroyed when the entire thread finishes executing)
Common Methods for threading:
Most common methods of threading exist in the thread class, so most of the time we call through the thread class.
1. Get the thread name
GetName ()
2. Get the current thread object
CurrentThread ()
3. Determine if the thread is started
isAlive ()
4. Forced execution of Threads
Join ()
5. Sleep on a thread
sleep ()
6. Comity of threads (very important for the declaration cycle)
yield ()
Code Demo: (1, 2)
Package Com.yeqc.thread;class Runabledemo implements Runnable{private string Name;public Runabledemo (string name) { THIS.name = name;} @Overridepublic void Run () {for (int i = 0; i < i++) {System.out.println ("Current thread object:" +thread.currentthread (). GetName () );}}} public class ThreadDemo02 {public static void main (string[] args) {Runabledemo r1 = new Runabledemo ("A"); Runabledemo r2 = new Runabledemo ("B"); thread T1 = new Thread (r1); Thread t2 = new Thread (r2); T1.start (); T2.start ();}}
Operation Result:
Current thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current thread object: Thread-0 Front Thread object: Thread-1 Current Threading object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Line Path object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current thread Object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-0 Current Thread object: Thread-0 Current thread pair Image: Thread-0 Current thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread Object : Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-0 Current Thread object: Thread-0 Current Thread object: Thread-0 Current Thread object: Thread-0 Current Thread object: Thread-0 Current Thread object: Thread-0 Current Thread object: Thread-0 Current Thread object: Thread-0 Current thread object: T hread-0 Current Thread object: Thread-0 Current thread object: THRead-0 Current Thread object: Thread-0 Current Thread object: Thread-0 Current Thread object: Thread-0 Current Thread object: Thread-0 Current Thread object: Thread-0 Current Thread object: Thread-0 Current Thread object: Thread-0 The current thread object: thre ad-0 Current Thread object: Thread-0 Current Thread object: Thread-0 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-1 The current thread object: Threa D-1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current thread object: Thread 1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current Thread object: Thread-1 Current thread object: thread- 1 Current Thread object: Thread-1
Code Demo: (3. Determine if the thread is started)
Package Com.yeqc.thread;class Runabledemo implements Runnable{private string Name;public Runabledemo (string name) { THIS.name = name;} @Overridepublic void Run () {for (int i = 0; i < i++) {System.out.println (name+ ":" +i);}} public class ThreadDemo02 {public static void main (string[] args) {Runabledemo r = new Runabledemo ("A"); Thread t = new Thread (r); System.out.println (T.isalive ()); T.start (); System.out.println (T.isalive ());}}
Operation Result:
falsetruea:0a:1a:2a:3a:4a:5a:6a:7a:8a:9a:10a:11a:12a:13a:14a:15a:16a:17a:18a:19a:20a:21a:22a:23a:24a:25a:26a : 27a:28a:29a:30a:31a:32a:33a:34a:35a:36a:37a:38a:39a:40a:41a:42a:43a:44a:45a:46a:47a:48a:49
Code demo (4. Forced execution of threads)
Package Com.yeqc.thread;class Runabledemo implements Runnable{private string Name;public Runabledemo (string name) { THIS.name = name;} @Overridepublic void Run () {for (int i = 0; i < i++) {System.out.println (name+ ":" +i);}} public class ThreadDemo02 {public static void main (string[] args) {Runabledemo r = new Runabledemo ("A"); Thread t = new Thread (r), T.start (), for (int i=0; i<50; i++) {if (i>10) {try {t.join ();} catch (Interruptedexception e) {E.printstacktrace ();}} System.out.println ("Main thread:" +i);}}}
Operation Result:
Main thread: 0a:0 main thread: 1a:1 main thread: 2a:2 main thread: 3a:3 main thread: main thread of 4a:4:5a:5 main thread: 6a:6 main thread: 7a:7 main thread: 8a:8 main thread: 9a:9 : 15a:16a:17a:18a:19a:20a:21a:22a:23a:24a:25a:26a:27a:28a:29a:30a:31a:32a:33a:34a:35a:36a:37a:38a:39a:40a:41a : 42a:43a:44a:45a:46a:47a:48a : 49 Main Thread: 11 Main thread: 12 main thread: 13 main thread: 14 main thread: 15 main thread: 16 main thread: 17 main thread: 18 main thread: 19 main thread: 20 main thread: 21 main thread: 22 main thread: 23 main thread: 24 main thread: 25 main thread: 26 main thread £ º 27 main thread: 30 Main thread: 31 Main Threads: 32 main Thread: 33 main thread: 34 main thread: 35 main thread: 36 main thread: 37 main thread: 38 main thread: 39 main Thread: 40 main thread: 41 main thread: 42 main thread: 43 main thread: 44 main thread: 45 main thread
Code demo (5. Thread hibernation)
Package Com.yeqc.thread;class Runabledemo implements Runnable{private string Name;public Runabledemo (string name) { THIS.name = name;} @Overridepublic void Run () {for (int i = 0; i <; i++) {try {thread.sleep (1000); System.out.println (name+ ":" +i);} catch (Interruptedexception e) {e.printstacktrace ();}}}} public class ThreadDemo02 {public static void main (string[] args) {Runabledemo r = new Runabledemo ("A"); Thread t = new Thread (r); T.start ();}}
Run results (once per 1s)
a:0a:1a:2a:3a:4a:5a:6a:7a:8a:9a:10a:11a:12a:13a:14a:15a:16a:17a:18a:19a:20a:21a:22a:23a:24a:25a:26a:27a:28a : 29a:30a:31a:32a:33a:34a:35a:36a:37a:38a:39a:40a:41a:42a:43a:44a:45a:46a:47a:48a:49
Code demo (6. Thread comity)
Package Com.yeqc.thread;class Runabledemo implements Runnable{private string Name;public Runabledemo (string name) { THIS.name = name;} @Overridepublic void Run () {for (int i = 0; i < i++) {System.out.println (name+ ":" +i), if (i = =) {System.out.print ln ("comity"); Thread.yield ();}}} public class ThreadDemo02 {public static void main (string[] args) {Runabledemo r1 = new Runabledemo ("A"); Runabledemo r2 = new Runabledemo ("B"); thread T1 = new Thread (r1); Thread t2 = new Thread (r2); T1.start (); T2.start ();}}
Operation Result:
A:0b:0a:1a:2a:3b:1a:4b:2a:5b:3a:6b:4a:7b:5a:8b:6a:9b:7a:10b:8 comity b:9a:11b:10 comity a:12b:11a:13b:12a:14b:13a:15b:14a : 16b:15a:17b:16a:18b:17a:19b:18a:20b:19a:21b:20a:22b:21a:23b:22a:24b:23a:25b:24a:26b:25a:27a:28b:26a:29b:27a : 30b:28a:31b:29a:32b:30a:33b:31a:34b:32a:35b:33a:36b:34a:37b:35a:38b:36a:39b:37a:40b:38a:41b:39a:42b:40a:43b : 41a:44b:42a:45a:46b:43b:44b:45b:46a:47a:48b:47b:48b:49a:49
Java multithreaded Programming (ii) Operational status and common methods