09_多线程
1进程
在操作系统中一个可执行文件就是一个进程。
DOS操作系统称为单用户单进程OS;
Windows操作系统称为多用户多进程的OS。
真实意义上说,在单核的CPU一个时间点上只能执行一个程序,但CPU执行速度非常快,一秒钟能执行上亿次。OS有一个核心模块,叫CPU时钟管理器,它把一秒分成很多份,人类无法感觉出来。
2线程
线程:一个程序执行的多条路径。
创建方法(两种):
(1)创建一个类,实现Runnable接口,实现run方法。
import java.io.*;
public class Test throws Exception{
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread("t1"));
t1.start();
Thread t2 = new Thread(new MyThread("t1"));
t2.start();
}
}
class MyThread implement Runnable{
String name;
public MyThread(String name){
this.name = name;
}
public void run() {
for(int i = 0;i<100;i++) {
System.out.println(name + ":" + i);
}
}
}
(2)Thread类本身实现了Runnable接口,创建一个类直接继承Thread类。
import java.io.*;
public class Test throws Exception{
public static void main(String[] args) {
MyThread t1 = new MyThread("t1");
t1.start();
MyThread t2 = new MyThread("t2");
t2.start();
}
}
class MyThread extends Thread{
String name;
public MyThread(String name){
this.name = name;
}
public void run() {
for(int i = 0;i<100;i++) {
System.out.println(name + ":" + i);
}
}
}
注意:用第一种方法更好,应为继承只能单继承,而接口能够多实现。
3线程死锁
代码模拟:
import java.io.*;
public class Test{
public static void main(String[] args) {
Thread t1 = new Thread(new T("t1"));
Thread t2 = new Thread(new T("t2"));
t1.start();
t2.start();
}
}
class T implements Runnable{
String name;
static Object o1 = new Object();
static Object o2 = new Object();
public T(String name){
this.name = name;
}
public void run(){
if("t1".equals(name)) {
synchronized(o1){
System.out.println("线程t1锁住了o1");
synchronized(o2){
System.out.println("线程t1锁住了o2");
}
}
}else if("t2".equals(name)){
synchronized(o2){
System.out.println("线程t2锁住了o2");
synchronized(o1){
System.out.println("线程t2锁住了o1");
}
}
}
}
}
4生产者—消费者问题
import java.io.*;
import java.util.*;
public class Test{
public static void main(String[] args) {
Repository repository = new Repository();
Thread producer = new Thread(new Producer(repository));
Thread consumer1 = new Thread(new Consumer(repository));
Thread consumer2 = new Thread(new Consumer(repository));
producer.start();
consumer1.start();
consumer2.start();
}
}
class Iphone{
int no;
public Iphone(int no){
this.no = no;
}
public String toString(){
return "Iphone" + no;
}
}
class Repository{
List<Iphone> list1 = new ArrayList<Iphone>();
public synchronized void add(Iphone iphone){
while(list1.size()==5){
try{
this.wait();
}catch(InterruptedException e){
}
}
this.notifyAll();
list1.add(iphone);
}
public synchronized Iphone get(){
while(list1.size()==0){
try{
this.wait();
}catch(InterruptedException e){
}
}
this.notifyAll();
Iphone iphone = list1.get(list1.size()-1);
list1.remove(list1.size()-1);
return iphone;
}
}
class Producer implements Runnable{
Repository repository;
public Producer(Repository repository){
this.repository = repository;
}
public void run(){
for(int i=1;i<=40;i++){
Iphone iphone = new Iphone(i);
repository.add(iphone);
System.out.println("生产了"+iphone);
try{
Thread.sleep((int)(Math.random()*300));
}catch(InterruptedException e){
}
}
}
}
class Consumer implements Runnable{
Repository repository;
public Consumer(Repository repository){
this.repository = repository;
}
public void run(){
for(int i=0;i<20;i++){
Iphone iphone = repository.get();
System.out.println("消费了"+iphone);
try{
Thread.sleep((int)(Math.random()*300));
}catch(InterruptedException e){
}
}
}
}
浙公网安备 33010602011771号