多线程总结笔记

进程与多线程

进程是程序的一次动态执行过程,需要从经历代码加载,代码执行到执行完毕的一个完整过程。

多线程能同时运行多个进程。

多线程说实现并发机制的一种有效手段,线程和进程一样,都是实现并发的一个基本单位。

通过继承thrund来实现多线程的定义

一个类只要继承了thread类,此类就称为多线程实现类。在Thread类中必须明确覆写Thread中的run()方法,此方法称为线程的主体。

extends Thread

package Thread;

public class thread1 extends Thread {

  private String name;

  public thread1(String name){ //用构造方法设置属性内容

​    this.name=name;

  }

  @Override

  public void run() {

​    for (int i = 0; i <10; i++) {

​      System.*out*.println(name + "运行,i=" +i);

​    }

  }

}

package Thread;

public class ThreadDemo1 {

  public static void main(String[] args) {

​    thread1 mt1 =new thread1("线程1");

​    thread1 mt2 =new thread1("线程2");

​    mt1.run();

​    mt2.run();

  }

}


顺序执行,如果要正确的启动多线程,是不能调用run()方法的。任何情况下,thrund多线程都是调用start函数

public class ThreadDemo1 {

  public static void main(String[] args) {

​    thread1 mt1 =new thread1("线程A");

​    thread1 mt2 =new thread1("线程B");

​    mt1.start();

​    mt2.start();

  }

}

结果是交替运行,虽然调用了start()方法,但是主体是run()方法。

如果一个类通过继承thread类实现,那么只能调用一次start()方法,如果调用多次就会出现“illegalThradStartException”异常。

继承runnable接口实现多线程

implements Runnable

public class Mythread2 implements Runnable{

​    private String name;

​    public Mythread2(String name){ //用构造方法设置属性内容

​      this.name=name;  //为name属性赋值

​    }

​    @Override

​    public void run() {

​      for (int i = 0; i <10; i++) {

​        System.*out*.println(name + "运行,i=" +i);

​      }

​    }

  }

public class RunnableDemo {

  public static void main(String[] args) {

​    Mythread2 mt1 = new Mythread2("线程A”); 实例化runnable 子类对象

​    Mythread2 mt2 = new Mythread2("线程B");

​    Thread t1 = new Thread(mt1);  实例化thread类对象

​    Thread t2 = new Thread(mt2);

​    t1.start();

​    t2.start();

  }

}


thread类和runnable接口之间在使用上也是有区别的,如果一个类继承了Thread类,则不适合多个线程共享资源,而实现了runnable接口的话,就可以方便的实现资源共享。

public class Mythread3 implements Runnable{

  private int ticket = 5;

  public void run(){

​    for (int i = 0; i < 100; i++) {

​      if(ticket>0){

​        System.*out*.println("卖票:ticket=" + ticket--);

​      }

​    }

  }

public class RunnableDemo2 {

  public static void main(String[] args) {

​    Mythread3 my = new Mythread3();

​    new Thread(my).start();  //启动三个线程,实现资源共享

​    new Thread(my).start();

​    new Thread(my).start();

  }

}


判断线程是否启动

isAlive()方法测试线程是否已经启动,而且仍在运行。

线程的强制运行

Join() 方法让线程强制运行,在这期间,其他线程无法运行,

public class RunnableJoin {

  public static void main(String[] args) {

​    Mythread4 mt = new Mythread4();

​    Thread t = new Thread(mt,"线程");

​    t.start();

​    for (int i = 0; i < 10 ; i++) {

​      if(i>5){

​        try {

​          t.join();

​        }catch (Exception e){}

​      }

​      System.*out*.println("main " + i);

​    }

  }

};

线程休眠

Thread.sleep()方法实现线程的休眠。

中断线程

interrupt()

public class Mythread5 implements Runnable {

  @Override

  public void run() {

​    System.*out*.println("1, 进入run方法");

​    try{

​     Thread.*sleep*(1000);

​      System.*out*.println("2,完成休眠");

​    }catch (Exception e){

​      System.*out*.println("3.休眠被终止");

​      return;

​    }

​    System.*out*.println("4.run方法被终止");

  }

}

public class ThreadInterrupt {

  public static void main(String[] args) {

​    Mythread5 my = new Mythread5();

​    Thread t = new Thread(my , "线程");

​    t.start();

​    try {

​      Thread.*sleep*(2000);

​    }catch (Exception e){

​    }

​    t.interrupt();

  }

}

后台线程

setDaemon()

线程的优先级

Setpriority()方法可以设置一个线程的优先级,

线程的同步与死锁

同步就是指多个操作在同一个时间段内只能有一个线程进行,其他线程要等待此线程完成之后才可以继续执行。

同步代码块 synchronized(同步对象){需要同步的代码}

同步方法 synchronized 方法返回值 方法名称(参数列表){}

死锁指两个线程都在等待对方先完成,造成了程序的停滞。

原文地址:https://www.cnblogs.com/Lilwhat/p/13234968.html