Java 多线程

Java中的多线程,首先,在理解线程之前,要明白进程,一个进程有多个线程,线程是进程的实体。进程是交替运行的,所以我在写博客的时候,可以一边听着轻音乐,一边挂着qq。实际上CPU只执行一个线程,但是由于它的线程高速运转替换,所以我们才能为所欲为。

这就是线程的一个生命周期,主要有以下四种状态:

1.创建

2.可执行

3.非可执行

4.消亡

首先来看第一点——创建。

线程的创建有两种方式:①继承Thread类;②实现Runnable接口;

Thread类:Thread类中常用的方法包括start()方法,interrupt()方法,join()方法,run()方法;

构造方法:① Thread thread = new Thread();

               ② Thread thread = new Thread(Runnnable simple);

               ③ Thread thread = new Thread("ThreadName");

               ④ Thread thread = new Thread(Runnnable simple,String name);

 1 package jihe;
 2 
 3 public class Simple extends Thread{
 4     public Simple(String s){
 5         super(s);   //调用父类构造方法
 6     }
 7     
 8     public void run(){
 9         int i=0;
10         while(i++ < 5){
11             try{
12                 System.out.println(getName() + "执行步骤" + i);
13                 Thread.sleep(1000);
14             } catch (Exception e){
15                 e.printStackTrace();
16             }
17         }
18     }
19 
20     public static void main(String[] args) {
21         Simple thread1 = new Simple("线程1");
22         Simple thread2 = new Simple("线程2");
23         thread1.start();
24         thread2.start();
25     }
26 
27 }

Runnable接口:

 1 package jihe;
 2 
 3 public class Simple1 implements Runnable {
 4 
 5     @Override
 6     public void run() {
 7         int i=15;
 8         while(i-- >=1){
 9             try{
10                 System.out.print("*");
11                 Thread.sleep(1000);
12             } catch (Exception e){
13                 e.printStackTrace();
14             }
15         }
16         
17     }
18 
19     public static void main(String[] args) {
20         Thread thread1 = new Thread(new Simple1(),"1");
21         thread1.start();
22     }
23 }
View Code

可执行:

当线程启动start()方法后,执行run()方法,考虑优先级;

非可执行:

wait(),sleep();——进入非可执行状态

notify(),notifyAll(),interrupt();——进入可执行状态

join()方法挂起,如果A调用B的join()方法,直到B执行完毕,才会执行A。

线程同步:

synchronized(someobject){

        代码块

}

 1 package jihe;
 2 
 3 public class SyncThread extends Thread{
 4     private String cha;
 5     public SyncThread(String cha){
 6         this.cha =cha;
 7     }
 8     public void run(){
 9         PrintClass.printch(cha);
10     }
11 
12     public static void main(String[] args) {
13         SyncThread t1 = new SyncThread("Aa");
14         SyncThread t2 = new SyncThread("Bb");
15         t1.start();
16         t2.start();
17 
18     }
19     
20     static class PrintClass{
21         static  Object printer = new Object();
22         public static void printch(String cha) {
23             synchronized(printer){
24                 for(int i=1;i<5;i++){
25                     System.out.println(cha+" ");
26                     try{
27                         //System.out.println("*");
28                         Thread.sleep(1000);
29                     } catch (Exception e){
30                         e.printStackTrace();
31                     }
32                 }
33             }
34         }
35     }
36 
37 }
View Code

资源共享:Runnable可以实现资源共享,Thread不可以。

使用Runnable接口
◦Runnable适合多个相同程序代码的线程去处理同一资源的情况。把虚拟CPU(线程)同程序的代码、数据有效的分离,较好的体现了面向对象的设计思想。
◦避免由于Java的单继承特性带来的局限性。也就是如果新建的类要继承其他类的话,因为JAVA中不支持多继承,就只能实现java.lang.Runnable接口。
◦有利于程序的健壮性,代码能够被多个线程共享,代码与数据是独立的。
继承Thread类
◦不能再继承他类。
◦编写简单,可以直接操纵线程,无需使用Thread.currentThread()。
每天都要进步一点,积少以成多。。。
原文地址:https://www.cnblogs.com/zxcjj/p/5548591.html