java多线程四种实现模板

假设一个项目拥有三块独立代码块,须要执行,何时用多线程?java

这些代码块某些时候须要同时运行,彼此独立,那么须要用到多线程操做更快。。。多线程

这里把模板放在这里,须要用的时候寻找合适的来选用。ide

整体分为两种:函数

1、使用匿名内部类的方式(推荐)学习

    1)方式1开启多线程(Thread)测试

//使用第一种方式,开启线程
        new Thread()
        {
            public void run()
            {
                for(int i=0;i<100;i++)
                {
                    System.out.println(Thread.currentThread().getName()+"线程方式1"+ i);
                }

            }
        }.start();

   2)方式2内部开启线程(Runnable)spa

//使用第二种方式,开启线程
        Runnable r =new Runnable()
        {
            public void run()
            {
                for(int i=0;i<100;i++)
                {
                    System.out.println(Thread.currentThread().getName()+".....方式2++"+ i);
                }
            }
        }; //不可或缺
        new Thread(r).start();

2、经过在外部定义类,类的实例化开启线程线程

 3)使用第三种方式,类实例化开启线程(继承方式)code

类定义:blog

class ThreadTest extends Thread 
{
    public void run()
    {
            for(int i=0;i<100;i++)
            {
                System.out.println(Thread.currentThread().getName()+".....类的方式(extends)----"+ i);
            }
    }

}

主函数中调用:

//使用第三种方式,类实例化开启线程(继承方式)
       new ThreadTest().start();

4)使用第四种方式,类实例化开启线程(实现方式)

类定义:

class  RunnableTest implements Runnable
{
    public void run()
    {
        for(int i=0;i<100;i++)
        {
            System.out.println(Thread.currentThread().getName()+".........类的方式(implements)----++"+ i);
        }
    }
}

主函数调用:

 //使用第四种方式,类实例化开启线程(实现方式)
       new Thread(new RunnableTest()).start();

附测试代码:

 1 class ThreadUseDemo 
 2 {
 3     public static void main(String[] args) 
 4     {
 5         //System.out.println("Hello World!");
 6         //使用第一种方式,开启线程
 7         new Thread()
 8         {
 9             public void run()
10             {
11                 for(int i=0;i<100;i++)
12                 {
13                     System.out.println(Thread.currentThread().getName()+"线程方式1"+ i);
14                 }
15 
16             }
17         }.start();
18         //主线程在这里运行
19         for(int i=0;i<100;i++)
20         {
21             System.out.println(Thread.currentThread().getName()+"前台线程"+ i);
22         }
23 
24         //使用第二种方式,开启线程
25         Runnable r =new Runnable()
26         {
27             public void run()
28             {
29                 for(int i=0;i<100;i++)
30                 {
31                     System.out.println(Thread.currentThread().getName()+".....方式2++"+ i);
32                 }
33             }
34         }; //不可或缺
35         new Thread(r).start();
36 
37        //使用第三种方式,类实例化开启线程(继承方式)
38        new ThreadTest().start();
39        //使用第四种方式,类实例化开启线程(实现方式)
40        new Thread(new RunnableTest()).start();
41     }
42 }
43 
44 class ThreadTest extends Thread 
45 {
46     public void run()
47     {
48             for(int i=0;i<100;i++)
49             {
50                 System.out.println(Thread.currentThread().getName()+".....类的方式(extends)----"+ i);
51             }
52     }
53 
54 }
55 class  RunnableTest implements Runnable
56 {
57     public void run()
58     {
59         for(int i=0;i<100;i++)
60         {
61             System.out.println(Thread.currentThread().getName()+".........类的方式(implements)----++"+ i);
62         }
63     }
64 }
ThreadUseDemo .java

线程学习告一段落了,以后关于线程其余的知识点,这里记下笔记,用的时候再说:

1)线程中止

经过在主函数中控制标志位来停止子线程的循环状态,特殊状况:

当现场处于冻结状态(wait)时,就读取不到flag标记,那么线程就不会结束。解决办法:

Interrupt:清楚冻结状态,固然此时会抛异常,在异常中更改标志位便可

   wait  ---

   sleep ----===》一砖头下去---》清醒(运行态)抛异常->有人强制结束,能够获取运行资格,操做标记为false,循环判断为假,线程结束

2) 守护线程

eg:   t1.setDaemon(true);//此时t1线程为守护线程,开启后和前台线程共同运行,互抢CPU资格,但当主线程(前台)结束后,守护线程也自动中止(依赖于前台主线程)

3) join()

eg:  t1.start();t1.join();//t1在start后,join表示向CPU申请执行权(CPU交出,处于wait状态),t1和其余正在运行的线程一块儿争夺,直到t1结束后交还资格给CPU

4) 线程组:谁开启的线程,就属于某个组(几乎用不到)

5)线程优先级:1--10,默认为5,常常用获得有:MIN_PRIORITY (1);MAX_PRIORITY (10); NORM_PRIORITY (5 )

定义:

这是java线程的优先级: 
java.lang.Thread 
public static final int MAX_PRIORITY 10 
public static final int MIN_PRIORITY 1 
public static final int NORM_PRIORITY 5 

使用:

 1 //第一种方案
 2 class MyThead implements Runnable
 3 {
 4     public void run()
 5     {
 6         for (int i = 1; i <= 10; i++)
 7         {
 8             System.out.println(Thread.activeCount() + "thread======>AAA");
 9         }
10     }
11 }
12 //第二种方案
13 class MyThreadRunnable extends Thread
14 {
15 
16     public void run()
17     {
18         for (int i = 1; i <= 10; i++)
19         {
20             System.out.println(Thread.activeCount() + "thread======BBB");
21         }
22     }
23 
24 }
25 
26 public class TheadMain
27 {
28     public static void main(String[] args)
29     {
30         MyThead myThead = new MyThead();
31         Thread thread = new Thread(myThead);
32         MyThreadRunnable thread2 = new MyThreadRunnable();
33         thread.start();
34         thread.setPriority(Thread.MIN_PRIORITY);
35         thread2.start();
36         thread2.setPriority(Thread.MAX_PRIORITY);
37     }
38 }
相关文章
相关标签/搜索