多线程

来源:互联网 发布:apache kylin 安装 编辑:程序博客网 时间:2024/06/05 17:40

一、进程的概念:

程序的一次运行叫做一个进程 程序中至少包含一个进程

二、线程

一个进程中至少包含一个线程,线程是进程的程序中的不同执行路径

三、多进程:在操作系统中同时运行多个任务,比如qq

四、多线程:一个程序中不同的执行路径,注意分支不是多线程,只是一个线程

五、线程和进程的区别

1.定义区别

2.从系统开销上来说

1.进程的开销比较大,虚拟机会为这个进程分配5个内存空间

 2.线程的开销小,启动一个新的线程会为这个线程开一个虚拟栈,多个线程共享一个堆和方法区的

六、进程调度算法

1.FCFS先来先服务

2.短进程优先 优先使用cpu

3.时间片轮转

4.优先级高者优先

六.操作系统的几种管理方式

1.进程管理

2.内存管理

3.文件管理

4.设备管理

5.作业管理

七、实现多进程的两种方法

//多线程的第一种实现方式:继承Thread类public class 线程的第一种方法 extends Thread {  private int i;  //第一步:继承Thread类 从写run()方法  public void run(){  for(;i<100;i++){  System.out.println(getName()+" "+i);  }  }  public static void main(String[] args) {  {  for(int i=0;i<100;i++){  System.out.println(Thread.currentThread().getName()+" "+i); if(i==20){ //第二步、通过创建Thread类的对象来创建新的对象 new  线程的第一种方法().start(); new  线程的第一种方法().start(); //第三步:调用Thread类对象的Start方法来启动一个线程 }  }  }}
//线程的第二种方法 实现runable接口 重写run方法public class 线程第二种方法 implements Runnable {private int i=0;@Override//第一步 实现runable接口 重写run方法//通过Thread.currentThread().getName()来获得当前线程名字public void run() {  for(;i<100;i++){  System.out.println(Thread.currentThread().getName()+" "+i);}}  //第二步:先创建实现了Runable接口类的对象r//创建thread对象并把r作为参数传给Thread类的构造函数public static void main(String[] args) {线程第二种方法 r=new 线程第二种方法();Thread t1=new Thread(r,"线程1");Thread t2=new Thread(r,"线程2");t1.start();t2.start();//第三步,调用Thread对象的Start方法}
//两种方法推荐使用第二种方法 因为java是单继承,使用第一种继承Thread类这种方式,不能继承其他类了
//继承Thread类无法共享数据 对象只能用自己的属性 使用接口,如果两个线程对象都是使用同一个Runable对象,可以共享
八.同步方法的原理:锁定当前对象 拿当前对象作为一把锁  this 1开 0关
/同步方法的原理:锁定当前对象 拿当前对象作为一把锁  this 1开 0关//加了synchronized加了锁以后 调用这个方法 要先判断这把锁是开或者关//实现同步的两种方式:一、同步方法:pubilc synchronized test(){} 2.同步代码块synchronized(this){}3.同步锁public class 同步方法 implements Runnable{private int sum=8000;private ReentrantLock lock = new ReentrantLock();@Overridepublic void run() {withdraw();}/*public synchronized void withdraw(){System.out.println("进入了withdraw");if(5000<=sum){try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}sum=sum-5000;System.out.println("出5000");}else{System.out.println("余额不足");}}*///同步方法/*public void withdraw(){synchronized (this) {System.out.println("进入了withdraw");if(5000<=sum){try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}sum=sum-5000;System.out.println("出5000");}else{System.out.println("余额不足");}}}//同步代码块*/public void withdraw(){lock.lock();System.out.println("进入了withdraw");try{if(5000<=sum){try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}sum=sum-5000;System.out.println("出5000");}else{System.out.println("余额不足");}}finally{lock.unlock();}}//方法锁public static void main(String[] args) {同步方法 s=new 同步方法();Thread t1=new Thread(s);Thread t2=new Thread(s);t1.start();t2.start();try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}  System.out.println("账户余额为:"+s.getSum());}public int getSum() {return sum;}public void setSum(int sum) {this.sum = sum;}}
九.多线程基本方法
1.isAlive()判断线程是否存活
代码如下:
public class 判断线程是否活着  extends Thread{ int i=0;    public void run(){    System.out.println("程序正在运行:"+Thread.currentThread().isAlive());    while(i<=200){    System.out.println(this.getName()+i);    i++;    }    try {Thread.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}    }    public static void main(String[] args) {System.out.println("程序正在运行:"+Thread.currentThread().isAlive());判断线程是否活着 s =new 判断线程是否活着();      System.out.println("s线程处于新建状态"+s.isAlive());      s.start();      System.out.println("s线程处于就绪状态:"+s.isAlive());      try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}    System.out.println("s线程阻塞了"+s.isAlive());    try {Thread.sleep(600);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}    System.out.println("s线程结束了"+s.isAlive());    }}
2.优先级代码如下:
public class 优先级  implements Runnable{private int i;@Overridepublic void run() {for(i=0;i<=100;i++){System.out.println(Thread.currentThread().getName()+" "+i);}}public static void main(String[] args) {优先级 s=new 优先级();Thread a=new Thread(s,"线程1");Thread b=new Thread(s,"线程2");a.setPriority(Thread.MIN_PRIORITY);b.setPriority(Thread.MAX_PRIORITY);a.start();b.start();}}
3.Join线程合并(相当于方法调用)
//Join线程合并(相当于方法调用)public class 线程合并  implements Runnable{private int i;@Overridepublic void run() { while(i<=100){ System.out.println(Thread.currentThread().getName()+" "+i); i++; }}public static void main(String[] args) {线程合并 s=new 线程合并();Thread a=new Thread(s);a.start();try {a.join();} catch (InterruptedException e) {e.printStackTrace();}}   }
4.yield()线程打断
public class Test {public static void main(String[] args) {   NumTread a=new NumTread();   StringTread b=new StringTread();   a.start();   b.start();   }    static class NumTread extends Thread{int i=0;@Overridepublic void run() {while(i<200){if(i%500==0){yield();}System.out.println("我在打印数字:"+i++);}}}static class StringTread extends Thread{int i=0;@Overridepublic void run() {while(i<200){System.out.println("我在打印字符串:"+i++);}}}}