java多线程四种实现模板

假设一个项目拥有三块独立代码块,需要执行,什么时候用多线程?

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

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

总体分为两种:

一、使用匿名内部类的方式(推荐)

    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)

//使用第二种方式,开启线程
        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();

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

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

类定义:

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 }

  

原文地址:https://www.cnblogs.com/shuqingstudy/p/5060499.html