09_1_Basic concept of thread

09_1_Basic concept of thread

1.  The basic concept of thread

Sequential control flow within a program of a thread.

difference between thread and process

Each process has independent code and data space ( process context ) , and switching between processes will have a large overhead.

Threads can be regarded as lightweight processes, the same type of threads share code and data space, each thread has an independent running stack and program counter (PC) , and the overhead of thread switching is small.

Multiprocessing: Running multiple tasks ( programs ) simultaneously in a program system .

Multithreading: There are multiple sequential flows executing concurrently in the same application

Java threads are implemented through the java.lang.Thread class.

When the VM starts, there is a thread defined by an autonomous method (public static void main() {}) .

New threads can be created by creating an instance of Thread .

Each thread completes its operation through the method run() corresponding to a specific Thread object , and the method run() is called the thread body.

A thread is started by calling the start() method of the Thread class .

1.1 One of two ways to implement threads

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 < 1000; i++) {

System.out.println("Main Thread" + i);

}

}

}

class Runner1 implements Runnable {

 

@Override

public void run() {

for (int i = 0; i < 1000; i++) {

System.out.println("Runner1 Thread" + i);

}

}

 

}

 

 

1.2 One of two ways to implement threads

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 {

 

@Override

public void run() {

for (int i = 0; i < 1000; i++) {

System.out.println("Runner1 Thread" + i);

}

}

 

}

 

2.  sleep method

sleep method

Static methods of Thread can be called :

public static void sleep(long millis) throws InterruptedException

Make the current thread sleep ( temporarily stop execution for millis milliseconds )

Since 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(10000);

} catch (InterruptedException e) {

}

t.interrupt();

}

}

class Runner3 extends Thread {

 

@Override

public void run() {

for (int i = 0; i < 1000; i++) {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

return;

}

System.out.println("-------------" + new Date() + "---------------------");

}

}

 

}

 

 

3.  The producer-consumer problem

 

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];
	
	public synchronized void push(WoTou wt) {
		while(index == arrWT.length) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace ();
			}
		}
		this.notifyAll();		
		arrWT[index] = wt;
		index ++;
	}
	
	public synchronized WoTou pop() {
		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 ();
			}			
		}
	}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325063736&siteId=291194637