一.多线程
     1.基本概念
         进程:正在运行中的程序,一个进程中至少包含一个线程
         线程:进程的任务,执行任务的一个通道,一个进程中可以包含多个线程
     2.多线程执行的特点:
         两种方式:分时调度/抢占式调度(java属于抢占)

二.Thread 类(java.lang)
     1.概述:使用该类表示多线程对象,只要创建一个Thread对象,就是一个线程对象产生了
     2.定义:public class Thread extends Object implements Runnable
     3.构造方法:
         Thread():无参构造,分配新的 Thread 对象
         Thread(String name):使用指定的名称分配新的 Thread 对象,设置线程名称
         Thread(Runnable target):接收Runnable接口子类对象,实例化Thread对象
         Thread(Runnable target, String name):接收Runnable接口子类对象,实例化Thread对象,并设置线程名称
     4.静态方法:
         public static Thread currentThread(){}:返回目前正在执行的线程 [线程名,优先级,]
         public static void sleep(long millis) throws InterruptedException{}:使当前线程休眠多少毫秒
         public static void yield(){}:将目前执行的线程暂停,允许其它线程执行
    
     5.常用方法:
         public void run(){}:如果该线程是使用独立的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run
                             方法;否则,该方法不执行任何操作并返回。<所以该方法应自觉重写!!!>
         public void start(){}:使该线程开始执行;Java 虚拟机调用该线程的 run 方法
         public final String getName(){}:返回线程的名称
         public final int getPriority(){}:返回线程优先级
         public final void setName(String name){}:设定线程名称
         public final void setPriority(int newPriority){}:设定线程的优先值
         public final ThreadGroup getThreadGroup(){}:Returns the thread group to which this thread belongs.
                                             This method returns null if this thread has died (been stopped).
     代码演示:
       

  1  //多线程基本练习-Thread类
2 class MyThread extends Thread{
3 //重写run()
4 @Override
5 public void run(){
6 for(int i = 0; i < 100; i++){
7 System.out.println(Thread.currentThread()+"Jack"+i);
8 }
9 }
10 }
11
12 public class ThreadDemo{
13 public static void main(String[] args){
14 //创建一个线程
15 MyThread my = new MyThread();
16 //启动线程
17 my.start();
18
19 //主线程执行如下任务
20 for(int i = 0; i<100; i++){
21 System.out.println(Thread.currentThread()+"肉丝"+i);
22 }
23
24 //返回当前运行的线程名称
25 String s = my.getName();
26 System.out.println("当前线程名称为:"+s);//Thread-0
27
28 //修改线程名称
29 my.setName("Smith--");
30 System.out.println("修改后-当前线程名称为:"+my.getName());
31
32 //返回线程优先级
33 int i = my.getPriority();
34 System.out.println("当前线程优先级为:"+i);//5
35 }
36 }

三.Runnable 接口(java.lang)
     1.概述:Runnable 接口只有一个方法,run方法,因此实现Runnable接口的类,必须重写run方法,否则,语法报错;
     2.实现接口的好处:
         由于类只能是单继承(即只出现一次extends),若使用接口,则实现类可以继承其他类,不占用继承的位置;
         可以多实现,可以将编写线程类和写任务代码(run()方法)的工作分离开;
     3.定义:
         @FunctionalInterface
         public interface Runnable
     4.方法:
         public void run() 使用实现接口 Runnable 的对象创建一个线程时,启动该线程将导致在独立执行的线程中调用对象的 run 方法

代码演示:
    

  1 //通过Runnable接口实现多线程
2 class MyRunnable implements Runnable{
3 @Override
4 public void run(){
5 for(int i = 0; i<100; i++){
6 System.out.println("Smith----------"+i+Thread.currentThread());
7 }
8 }
9 }
10
11 public class RunnableDemo{
12 public static void main(String[] args){
13 MyRunnable my = new MyRunnable();
14 Thread myThread = new Thread(my,"smith");
15 myThread.start();
16
17 //返回当前线程名称
18 System.out.println("当前线程:"+Thread.currentThread());
19
20 for(int i = 0;i<100;i++){
21 System.out.println("格林:==="+i+Thread.currentThread());
22 }
23 }
24 }

死锁现象:
    

  1 class Zhangsan{
2 public void say(){
3 System.out.println("张三对李四说:你给你画,我给你书");
4 }
5 public void get(){
6 System.out.println("张三得到画了");
7 }
8 }
9
10 class Lisi{
11 public void say(){
12 System.out.println("李四对张三说:你给我书,我就给你画");
13 }
14 public void get(){
15 System.out.println("李四得到书了");
16 }
17 }
18 //单例-饿汉式
19 public class ThreadDeadLock implements Runnable{
20 private static Zhangsan zs = new Zhangsan();
21 private static Lisi ls = new Lisi();
22 private boolean flag = false;
23 @Override
24 public void run(){
25 if(flag){
26 synchronized(zs){ //单例模式,所以实例化对象只有一个,可以作为锁使用
27 zs.say();
28 try{
29 Thread.sleep(500);
30 }catch(InterruptedException e){
31 e.printStackTrace();
32 }
33 synchronized(ls){
34 zs.get();
35 }
36 }
37 }else{
38 synchronized(ls){
39 ls.say();
40 try{
41 Thread.sleep(500);
42 }catch(InterruptedException e){
43 e.printStackTrace();
44 }
45 synchronized(zs){
46 ls.get();
47 }
48 }
49 }
50 }
51 public static void main(String[] args){
52 ThreadDeadLock t1 = new ThreadDeadLock();
53 ThreadDeadLock t2 = new ThreadDeadLock();
54 t1.flag = true;
55 t2.flag = false;
56 Thread thA = new Thread(t1);
57 Thread thB = new Thread(t2);
58 thA.start();
59 thB.start();
60 }
61 }

四.线程安全问题
     原因:当多个线程使用共享的资源时,容易发生数据安全的问题;
     解决方案:
         Java 提供了3种解决安全问题的方式;
             1:同步代码块
             2:同步方法
             3:Lock 接口
     1.同步代码块
       

  1  /*使用3个线程,模拟3个窗口,卖100张票;
2 要求每个线程卖出去的票,不能重复且不能是无效票;
3 使用一个变量表示100张票,每个窗口卖出去一张票,就将该变量的值减一,直到0为止;
4 使用同步代码块
5 */
6 class MyRunnable implements Runnable{
7 int ticket = 100;//总票数,只能new一次该类,因为每创建一次对象就会有100张票
8 @Override
9 public void run(){
10 String name = Thread.currentThread().getName();//获取当前线程名
11 while(true){
12 synchronized(this){
13 if(ticket<=0){
14 break;
15 }else{
16 System.out.println(name+"卖出第"+ticket+"号票");
17 ticket--;
18 }
19 }
20 }
21
22 }
23 }
24
25 public class SellTicket{
26 public static void main(String[] args){
27 //创建任务对象
28 MyRunnable my = new MyRunnable();
29 //创建线程
30 Thread t1 = new Thread(my,"窗口1");
31 Thread t2 = new Thread(my,"窗口2");
32 Thread t3 = new Thread(my,"窗口3");
33 //开启线程
34 t1.start();
35 t2.start();
36 t3.start();
37 }
38 }

2.同步方法
        

  1 class MyRunnable implements Runnable{
2 //定义总票数为成员变量
3 int ticket = 100;
4 String name;
5 @Override
6 public void run(){
7 name = Thread.currentThread().getName();
8 //卖票任务
9 while(true){
10 sell();
11 if(ticket<=0){
12 break;
13 }
14 }
15 }
16 //同步方法
17 public synchronized void sell(){
18 if(ticket > 0){
19 System.out.println(name+"卖出第"+ticket+"号票");
20 ticket--;
21 }
22 }
23 }
24
25 public class SellTicket01{
26 public static void main(String[] args){
27 //创建任务对象
28 MyRunnable my = new MyRunnable();
29 //创建多线程
30 Thread t1 = new Thread(my,"窗口1");
31 Thread t2 = new Thread(my,"窗口2");
32 Thread t3 = new Thread(my,"窗口3");
33 //开启多线程
34 t1.start();
35 t2.start();
36 t3.start();
37 }
38 }

3.Lock 接口
       

  1  import java.util.concurrent.locks.Lock;
2 import java.util.concurrent.locks.ReentrantLock;
3 class MyRunnable implements Runnable{
4 //定义票数为成员变量
5 int ticket = 100;
6 //创建锁对象
7 private static final Lock lock = new ReentrantLock();
8 @Override
9 public void run(){
10 String name = Thread.currentThread().getName();
11 while(true){
12 lock.lock();
13 try{
14 if(ticket > 0){
15 System.out.println(name+"卖出了第"+ticket+"号票");
16 ticket--;
17 }else{
18 break;
19 }
20 }finally{
21 lock.unlock();
22 }
23 }
24 }
25 }
26
27
28 public class SellTicket02{
29 public static void main(String[] args){
30 //创建任务对象
31 MyRunnable my = new MyRunnable();
32 //创建多线程
33 Thread t1 = new Thread(my,"窗口1");
34 Thread t2 = new Thread(my,"窗口2");
35 Thread t3 = new Thread(my,"窗口3");
36 //开启多线程
37 t1.start();
38 t2.start();
39 t3.start();
40
41 }
42 }

五.使用匿名内部类实现多线程
    

  1 /*编写程序,创建两个线程对象,一根线程循环输出“播放背景音乐”,另一根线程循环输出
2 “显示画面”,要求线程实现Runnable接口,且使用匿名内部类实现*/
3 public class ThreadDemo003{
4 public static void main(String[] args){
5 new Thread(new Runnable(){
6 @Override
7 public void run(){
8 for(int i = 0; i < 100; i++){
9 System.out.println("播放背景音乐");
10 }
11 }
12 }).start();
13
14 new Thread(new Runnable(){
15 @Override
16 public void run(){
17 for(int i = 0; i <100; i++){
18 System.out.println("显示画面");
19 }
20 }
21 }).start();
22 }
23 }

  1  /*编写程序,创建两个线程对象,一根线程循环输出“播放背景音乐”,另一根线程循环输出
2 “显示画面”,要求使用Thread类,且使用匿名内部类实现*/
3 public class ThreadDemo03{
4 public static void main(String[] args){
5 new Thread(){
6 @Override
7 public void run(){
8 for(int i = 0; i<100; i++){
9 System.out.println("播放背景音乐");
10 }
11 }
12 }.start();
13
14 new Thread(){
15 @Override
16 public void run(){
17 for(int i = 0; i<100; i++){
18 System.out.println("显示画面");
19 }
20 }
21 }.start();
22 }
23 }
24

六.ThreadGroup 类(java.lang)
     简介:线程组:java允许对一批线程进行管理,使用ThreadGroup表示线程组,所有线程均有指定线程组,如果没有显式指定,则为默认线程组.默认情况下,子线程和创建他的父线程
         属于同一线程组.一旦某线程加入指定线程组内,该线程会一直属于该组,直至死亡,运行过程中不可改变.
     继承关系:java.lang.Object--java.lang.ThreadGroup
     定义:public class ThreadGroup extends Object implements Thread.UncaughtExceptionHandler
     构造器:
         ThreadGroup(String name): Constructs a new thread group.
         ThreadGroup(ThreadGroup parent, String name): Creates a new thread group.
     常用方法:
         public int activeCount(){}:Returns an estimate of the number of active threads in this thread group and its subgroups
         public int activeGroupCount(){}:Returns an estimate of the number of active groups in this thread group and its subgroups.
                                         Recursively iterates over all subgroups in this thread group.
         public int enumerate(Thread[] list) Throws SecurityException{}:
         public int enumerate(Thread[] list,boolean recurse)Throws SecurityException{}:Copies into the specified array every active thread
                                                 in this thread group. If recurse is true, this method recursively enumerates all subgroups of
                                                 this thread group and references to every active thread in these subgroups are also included.
                                                 If the array is too short to hold all the threads, the extra threads are silently ignored.
         public final String getName(){}:Returns the name of this thread group.
         public final ThreadGroup getParent()Throws SecurityException{}:Returns the parent of this thread group.
         public final boolean isDaemon(){}:Tests if this thread group is a daemon thread group
         public final void checkAccess() Throws SecurityException{}:Determines if the currently running thread has permission to modify this thread group.
         public final void setDaemon(boolean daemon)Throws SecurityException{}:Changes the daemon status of this thread group.   
    
     代码演示:获取当前系统内运行的所有线程组及线程名
       

  1  import java.util.List;
2 import java.util.ArrayList;
3 public class ThreadListDemo{
4 public static void main(String[] args){
5 for(String s : getThreadGroups(getRootThreadGroups())){
6 System.out.println(s);
7 }
8 }
9
10 //getRootThreadGroups()
11 public static ThreadGroup getRootThreadGroups(){
12 //get current threadgroup
13 ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
14 while(true){
15 if(rootGroup.getParent() != null){
16 rootGroup = rootGroup.getParent();
17 }else{
18 break;
19 }
20
21 }
22 return rootGroup;
23 }
24
25 //getThreadGroups()传入一个线程组,获取该组内所有子线程组
26 public static List<String> getThreadGroups(ThreadGroup group){
27 List<String> threadList = getThreads(group);//存子线程组名,调用getThreads方法,返回线程组内所有线程名
28 ThreadGroup[] groups = new ThreadGroup[group.activeGroupCount()];//活动的线程组名
29 int count = group.enumerate(groups,false);//复制子线程组到线程组数据,不递归复制
30 for(int i = 0; i< count; i++){
31 threadList.addAll(getThreads(groups[i]));
32 }
33 return threadList;
34 }
35
36
37 //传入一个线程组,返回该组内所有线程名
38 public static List<String> getThreads(ThreadGroup group){
39 List<String> threadList = new ArrayList<>();//存线程名
40 Thread[] list = new Thread[group.activeCount()];//活动线程
41 int count = group.enumerate(list,false);//复制当前进程到list中
42 for(int i = 0; i< count; i++){
43 threadList.add("名为:"+group.getName()+"的线程组,线程名为:"+list[i].getName());
44 }
45 return threadList;
46 }
47
48 }

七.Executor 接口(java.io.concurrent)

简介:线程池:由于线程涉及到与操作系统交互,所以启动一个新线程的成本比较高,因此,java提供了线程池机制来提高性能,尤其是当程序中需要大量生存期很短暂的线程时,应该考虑
     使用线程池.所谓线程池是指在系统启动时即创建大量空闲的线程,程序将一个Runnable对象或Callable对象传给线程池,线程池就会启动一个线程来执行他们的run或call方法,当方法
     结束时,线程并不会死亡,而是返回线程池成为空闲状态,等待执行下一个任务

定义: public interface Executor

方法:public void execute(Runnable command) Throws RejectedExecutionException | NullPointerException {}:Executes the given command at some time in the future

实现类:AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor

子接口:ExecutorService, ScheduledExecutorService

多线程-Thread-Runnable的更多相关文章

  1. android 多线程Thread,Runnable,Handler,AsyncTask

    先看两个链接: 1.http://www.2cto.com/kf/201404/290494.html 2. 链接1: android 的多线程实际上就是java的多线程.android的UI线程又称 ...

  2. 第39天学习打卡(多线程 Thread Runnable 初始并发问题 Callable )

    多线程详解 01线程简介 Process与Thread 程序:是指令和数据的有序集合,其本身没有任何运行的含义,是一个静态的概念. 进程则是执行程序的一次执行过程,它是一个动态的概念.是系统资源分配的 ...

  3. java 多线程--- Thread Runnable Executors

    java 实现多线程的整理: Thread实现多线程的两种方式: (1)继承 Thread类,同时重载 run 方法: class PrimeThread extends Thread { long ...

  4. [java多线程] - Thread&Runnable运用

    负载是一个很大的话题,也是一个非常重要的话题.不管是在大的互联网软件中,还是在一般的小型软件,都对负载有一定的要求,负载过高会导致服务器压力过大:负载过低又比较浪费服务器资源,而且当高请求的时候还可能 ...

  5. Java多线程高并发学习笔记(一)——Thread&Runnable

    进程与线程 首先来看百度百科关于进程的介绍: 进程是一个具有独立功能的程序关于某个数据集合的一次运行活动.它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体.它不只是程序的代码,还包括当前的 ...

  6. JAVA多线程Thread VS Runnable详解

    要求 必备知识 本文要求基本了解JAVA编程知识. 开发环境 windows 7/EditPlus 演示地址 源文件   进程与线程 进程是程序在处理机中的一次运行.一个进程既包括其所要执行的指令,也 ...

  7. 多线程-Thread、Runnable 创建线程和调用过程分析

    创建线程的两种方式: 1:创建Thread类的子类 ---基于继承的技术 . 2:以Runnable接口实例为构造参数直接通过new  创建 Thread 实例.---基于组合的技术. public ...

  8. 多线程-----Thread类与Runnable接口的区别

    第一个继承Thread类来实现多线程,其实是相当于拿出三件事即三个卖早餐10份的任务分别分给三个窗口,他们各做各的事各卖各的早餐各完成各的任务,因为MyThread继承Thread类,所以在newMy ...

  9. java中多线程中Runnable接口和Thread类介绍

    java中的线程时通过调用操作系统底层的线程来实现线程的功能的. 先看如下代码,并写出输出结果. // 请问输出结果是什么? public static void main(String[] args ...

  10. 多线程 Thread.yield 方法到底有什么用?

    概念 我们知道 start() 方法是启动线程,让线程变成就绪状态等待 CPU 调度后执行. 那 yield() 方法是干什么用的呢?来看下源码. /** * A hint to the schedu ...

随机推荐

  1. POCO库中文编程参考指南(11)如何使用Reactor框架?

    1 Reactor 框架概述 POCO 中的 Reactor 框架是基于 Reactor 设计模式进行设计的.其中由 Handler 将某 Socket 产生的事件,发送到指定的对象的方法上,作为回调 ...

  2. hdu 3853 LOOPS (概率dp 逆推求期望)

    题目链接 LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Tota ...

  3. Docker环境下的前后端分离项目部署与运维(七)Redis高速缓存

    Redis高速缓存 利用内存保存数据,读写速度远超硬盘:可以减少I/O操作,降低I/O压力. 发红包.抢红包的数据可以存在高速缓存中,加快处理速度,不需要经过数据库 淘宝首页一些优惠活动商品等热数据可 ...

  4. 30个物联网传感器小实验:三行代码点亮LED灯

    30个物联网传感器小实验:三行代码点亮LED灯 三行代码点亮LED灯 LED灯闪烁 LED灯调亮度 LED淡入淡出 不写一行代码点亮LED灯 全彩RGB灯 面包板 30个物联网传感器小实验:三行代码点 ...

  5. Swoole WebSocket 的应用

    目录 概述 代码 小结 概述 这是关于 Swoole 学习的第三篇文章:Swoole WebSocket 的应用. 第二篇:Swoole Task 的应用 第一篇:Swoole Timer 的应用 什 ...

  6. Photoshop学习:打开PS之前需要...

    颜色:色相(色彩名称):赤橙黄... H 色彩饱和度(纯度):?% S 明度(明暗):B HSB:人眼所看到的 拾色器 色相环 中间亮 边缘饱和度 黑颜色无色相,灰度有 光的三原色:红绿蓝(RGB) ...

  7. __stdcall

    __stdcall是函数调用约定的一种,函数调用约定主要约束了两件事: 1.参数传递顺序 2.调用堆栈由谁(调用函数或被调用函数)清理 常见的函数调用约定:stdcall cdecl fastcall ...

  8. 剑指OFFER之打印1到最大的N位数(九度OJ1515)

    题目描述: 给定一个数字N,打印从1到最大的N位数. 输入: 每个输入文件仅包含一组测试样例.对于每个测试案例,输入一个数字N(1<=N<=5). 输出: 对应每个测试案例,依次打印从1到 ...

  9. [Swift]LeetCode1078. Bigram 分词 | Occurrences After Bigram

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  10. 黑马学习SpringMVC bug集锦X1