There are two ways to implement threading, one is to implement the Runnable interface, and one is to inherit the thread threading class. The difference between the two is that the former only implements a class for the Runnable interface, not a thread, which inherits thread
Let's take a look at the code for details
public class ThreadTest {public static void main (string[] args) {T1 t1=new T1 ("T1"); T2 t2=new T2 ("T2"), new Thread (T1), start (),//1 statement from the test code can see T2 is a real thread object and T1 is not T2.start (); The//2 statement here is worth noting that statement 1 is not finished Statement 2 begins execution of statement 1 and Statement 2 o'clock concurrent execution of}}class T1 implements Runnable {string Name;public T1 (string name) {this.name = name;} public void Run () {for (int i=0;i<5;i++) {System.out.println ("I Am" +name);}}} Class T2 extends Thread {string Name;public T2 (string name) {this.name = name;} public void Run () {for (int i=0;i<5;i++) {System.out.println ("I Am" +name);}}}
There are threads we can handle concurrently, but this will also allow the occurrence of reading dirty data, or losing updates, data confusion, for example, in the case of a two-thread simultaneous access to a common single-instance acquisition method in a singleton pattern. When a thread prepares to assign a value to an object reference, the thread reads the object's reference empty and prepares the object and assigning references to local variables.
This results in a single case of data confusion, two instances, and our intentions are inconsistent.
In order to synchronize, we introduced the Synchronized keyword to synchronize. The Synchronized keyword can be used either before the object or as a method adornment. Synchronized (object reference name) public synchronized void () {}
The Synchronized keyword guarantees the mutex of a resource, that is, only one thread can access it for a period of time. A resource lock must be held before a thread can access it.
Thread synchronization simply dictate that no code is sent. Let's look at the thread collaboration.
Here's the keyword Wait () notify () method thread collaboration Let's take a look at the classic code producer consumer
Public class Cooperation {public static void main (string[] args) {supermonitor monitor = new Supermonitor (); New Producer (M ONITOR,200); new Consumer (monitor,200); try{thread.sleep (1000);} catch (Interruptedexception e) {e.printstacktrace ();}}} Production consumption to balance production after consumption, after consumption after the reproduction by the Administrator class control classes Supermonitor {Boolean valueSet = false;//flag is produced in the production of int count;//The total amount of public supermo Nitor () {}public synchronized int get () {if (!valueset) {//production line no longer produced, then consumer thread enters the waiting pool, waits for the line to Wake Try{wait ();} catch (Interruptedexception e) {e.printstacktrace ();}} ValueSet = false; System.out.println ("Consumed" +count+ "product"); notify (); return count;} Public synchronized void set (int i) {if (ValueSet) {///How the production line can be produced, the production line goes into the waiting pool, waits for the consumer thread to wake try{wait ();} catch (Interruptedexception e) {//e.printstacktrace ();}} ValueSet = true;count=i; System.out.println ("produced" +count+ "Products"); notify (); }} class Producer implements Runnable{supermonitor monitor;int speed;//production speed correlation coefficient public Producer (Supermonitor Monitor , int speed) {this.monitor = Monitor;this.speed = Speed;new threAD (This, "Producer"). Start ();} public void Run () {int I=0;while (true) {Monitor.set (++i); Try{thread.sleep ((int) math.random () *speed);} catch (Interruptedexception e) {//e.printstacktrace ();}}} } class Consumer implements runnable{Supermonitor monitor; int speed;//consumption quantity correlation coefficient public Consumer (supermonitor monitor,int speed) {this.monitor = Monitor;this.speed = Speed;new Threa D (This, "Consumer"). Start ();} public void Run () {while (true) {monitor.get (); try {thread.sleep ((int) math.random () *speed);} catch (Interruptedexception e) {e.printstacktrace ();}} } }
The note here is that the Get set method in the Administrator class needs to be locked because it is possible to call the method at the same time in a multithreaded environment, otherwise it will cause an exception
Threading Knowledge Summary