美文网首页
Java多线程

Java多线程

作者: 发光时代或个人头一回 | 来源:发表于2018-02-04 17:37 被阅读0次

    1. 进程与线程

    线程:程序中单独的控制流

    线程本身依靠程序进行运行

    线程是程序中的顺序控制流,只能使用分配给程序的资源和环境

    进程:执行中的程序

    一个进程可以包含一个或多个线程

    一个进程至少包含一个线程

    单线程:程序中只存在一个线程,实际上主方法就是一个主线程

    多线程:在一个程序中运行多个任务

    目的是更好地使用CPU资源

    2. 线程的实现

    继承Thread类

    在java.lang包中定义, 继承Thread类必须重写run()方法

    实现Runnable接口

    3. 线程的状态

    创建状态: 准备好了一个多线程的对象

    就绪状态: 调用了start()方法, 等待CPU进行调度

    运行状态: 执行run()方法

    阻塞状态: 暂时停止执行, 可能将资源交给其它线程使用

    终止状态: 线程销毁

    4. 线程的常用方法

    取得线程名称:getName()

    取得当前线程对象:currentThread()

    判断线程是否启动:isAlive()

    线程的强制执行:join()

    手动强制执行另一个线程

    线程的休眠:sleep()

    常用. 读入参数毫秒.

    线程的礼让:yield()

    classRunnableDemoimplementsRunnable{privateString name;publicRunnableDemo(String name){this.name = name;    }@Overridepublicvoidrun(){for(inti =0; i <50; i++) {            System.out.println(name +":"+ i);if(i ==10) {                System.out.println("线程"+ name +"礼让");                Thread.yield();            }        }    }}publicclassThreadDemo{publicstaticvoidmain(String[] args){        RunnableDemo r1 =newRunnableDemo("A");        RunnableDemo r2 =newRunnableDemo("B");        Thread t1 =newThread(r1);        Thread t2 =newThread(r2);        t1.start();        t2.start();    }}// 执行结果B:0A:0B:1A:1A:2A:3A:4A:5A:6A:7A:8A:9A:10线程A礼让B:2A:11B:3A:12A:13A:14A:15A:16B:4A:17B:5B:6B:7B:8A:18B:9B:10线程B礼让A:19...

    5. 线程的优先级

    优先级设置:

    1--MIN_PRIORITY

    5--NORM_PRIORITY(默认)

    10--MAX_PRIORITY

    classRunnableDemoimplementsRunnable{@Overridepublicvoidrun(){for(inti =0; i <5; i++) {try{                Thread.sleep(1000);                System.out.println(Thread.currentThread().getName() +":"+ i);            }catch(InterruptedException e) {                e.printStackTrace();            }        }    }}publicclassThreadDemo{publicstaticvoidmain(String[] args){        Thread t1 =newThread(newRunnableDemo(),"A");        Thread t2 =newThread(newRunnableDemo(),"B");        Thread t3 =newThread(newRunnableDemo(),"C");        t1.setPriority(Thread.MIN_PRIORITY);        t2.setPriority(Thread.NORM_PRIORITY);        t3.setPriority(Thread.MAX_PRIORITY);        t1.start();        t2.start();        t3.start();    }}// 执行结果B:0A:0C:0B:1A:1C:1B:2A:2C:2B:3C:3A:3C:4A:4B:4// 从第4次往后C才获得优先执行, 所以设置优先级并不能保证线程的执行顺序.

    6. 同步

    资源共享的时候需要使用同步.

    同步代码块

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

    同步方法

    synchronizedvoidfun(){    }

    示例:

    classRunnableDemoimplementsRunnable{privateintticket =5;@Overridepublicvoidrun(){for(inti =0; i <10; i++) {synchronized(this) {if(ticket >0) {try{                        Thread.sleep(500);                    }catch(InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println("stcket: "+ ticket--);                }            }        }    }}publicclassThreadDemo{publicstaticvoidmain(String[] args){        RunnableDemo r =newRunnableDemo();        Thread t1 =newThread(r);        Thread t2 =newThread(r);        Thread t3 =newThread(r);        t1.start();        t2.start();        t3.start();    }}// 执行结果// 若不加synchronizedstcket:4stcket:5stcket:3stcket:2stcket:1stcket:0stcket: -1// 加上synchronizedstcket:5stcket:4stcket:3stcket:2stcket:1// 采用同步方法,结果一样classRunnableDemoimplementsRunnable{privateintticket =5;@Overridepublicvoidrun(){for(inti =0; i <10; i++) {            fun();        }    }publicsynchronizedvoidfun(){if(ticket >0) {try{                Thread.sleep(500);            }catch(InterruptedException e) {                e.printStackTrace();            }            System.out.println("stcket: "+ ticket--);        }    }}

    7. 线程的生命周期

    cycle.png

    注:sleep和wait的区别:

    sleep是Thread类的方法,wait是Object类中定义的方法.

    Thread.sleep不会导致锁行为的改变, 如果当前线程是拥有锁的, 那么Thread.sleep不会让线程释放锁.

    Thread.sleep和Object.wait都会暂停当前的线程. OS会将执行时间分配给其它线程. 区别是, 调用wait后, 需要别的线程执行notify/notifyAll才能够重新获得CPU执行时间.

    8. Thread类重要方法总结

    编号方法说明

    1public void start()使该线程开始执行;Java 虚拟机调用该线程的 run 方法。

    2public void run()如果该线程是使用独立的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,该方法不执行任何操作并返回。

    3public final void setName(String name)改变线程名称,使之与参数 name 相同。

    4public final void setPriority(int priority)更改线程的优先级。

    5public final void setDaemon(boolean on)将该线程标记为守护线程或用户线程。

    6public final void join(long millisec)等待该线程终止的时间最长为 millis 毫秒。

    7public void interrupt()中断线程。

    8public final boolean isAlive()测试线程是否处于活动状态。

    9public static void yield()暂停当前正在执行的线程对象,并执行其他线程。

    10public static void sleep(long millisec)在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。

    11public static Thread currentThread()返回对当前正在执行的线程对象的引用。

    相关文章

      网友评论

          本文标题:Java多线程

          本文链接:https://www.haomeiwen.com/subject/hqamzxtx.html