Basic concepts of 09_1_ threads 1.
Basic concepts of threading
The sequential control flow within a program of a thread.
The difference between threads and processes
separate code and data space for each process ( process Context ) , there is a significant overhead in switching between processes.
threads can be thought of as lightweight processes, sharing code and data spaces with a class of threads, each with separate run stacks and program counters (PC) , the overhead of thread switching is small.
Multi-process: multiple tasks running concurrently in a program system ( program ) .
Multithreading: Simultaneous execution of multiple sequential streams in the same application
Java threads are implemented through the java.lang.Thread class.
The VM starts with a thread defined by the Autonomic method (public static void Main () {}) .
can be created by creating instance of thread to create a new thread.
Each thread is passed through a specific The thread object corresponds to the method run () to complete its operation, and the method run () is called the thread body.
by calling The start () method of the thread class to start a thread.
1.1 One of two ways to implement threading
TestThread1
Package Test; public class TestThread1 {public static void main (string[] args) {Runner1 t = new Runner1 (); new Thread (t). Start () ; for (int i = 0; i < i++) {System.out.println ("Main Thread" + i);}}} Class Runner1 implements Runnable {@Overridepublic void Run () {for (int i = 0; i <; i++) {System.out.println ("Run Ner1 Thread "+ i);}} }
1.2 One of two ways to implement threading
TestThread2
Package Test; public class TestThread2 {public static void main (string[] args) {Runner2 t = new Runner2 (), T.start (); for (int i = 0; i < 1000; i++) {System.out.println ("Main Thread" + i);}}} Class Runner2 extends Thread {@Overridepublic void Run () {for (int i = 0; i <; i++) {System.out.println ("Runner1 Thread "+ i);}} }
2.
sleep Method
Sleep Method
can call static method of Thread:
public static void sleep (long Millis) throws Interruptedexception
causes the current thread to hibernate ( temporarily stop execution millis milliseconds )
because it is a static method,sleep can be called directly by the class name:
Thread.Sleep ()
Example:
Package Test; Import Java.util.Date; public class TestThread3 {public static void main (string[] args) {Runner3 t = new Runner3 (); T.start (); try {thread.sleep (1 0000);} catch (Interruptedexception e) {}t.interrupt ();}} Class Runner3 extends Thread {@Overridepublic void Run () {for (int i = 0; i <; i++) {try {Thread.Sleep (+)};} CA TCH (Interruptedexception e) {return;} System.out.println ("-------------" + new Date () + "---------------------");}} }
3.
Producer Consumer issues
public class Producerconsumer {public static void main (string[] args) {syncstack ss = new Syncstack (); Producer p = new Producer (ss); Consumer C = new Consumer (ss), new Thread (p). Start (); new Thread (P). Start (); new Thread (P). Start (); new Thread (c). Start ();}} Class Wotou {int id; Wotou (int id) {this.id = ID;} Public String toString () {return ' Wotou: ' + ID;}} Class Syncstack {int index = 0; wotou[] ARRWT = new Wotou[6];p ublic synchronized void push (Wotou wt) {while (index = = arrwt.length) {try {this.wait ()} cat CH (interruptedexception e) {e.printstacktrace ();}} This.notifyall (); Arrwt[index] = Wt;index + +;} Public synchronized Wotou POPs () {while (index = = 0) {try {this.wait ();} catch (Interruptedexception e) {e.printstacktrace () ;}} This.notifyall (); Index--;return arrwt[index];}} Class Producer implements Runnable {Syncstack SS = null; Producer (Syncstack ss) {THIS.SS = SS;} public void Run () {for (int i=0; i<20; i++) {Wotou wt = new Wotou (i); ss.push (WT); System.out.println ("produced:" + wt); try {Thread.Sleep((int) (Math.random () * 200)); catch (Interruptedexception e) {e.printstacktrace ();}}}} Class Consumer implements Runnable {Syncstack SS = null; Consumer (Syncstack ss) {THIS.SS = SS;} public void Run () {for (int i=0; i<20; i++) {Wotou wt = SS.POP (); System.out.println ("Consumed:" + wt); try {thread.sleep ((int) (Math.random () * 1000)); catch (Interruptedexception e) {e.printstacktrace ();}}}}
Basic concepts of 09_1_ threads