博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA--线程
阅读量:4634 次
发布时间:2019-06-09

本文共 4376 字,大约阅读时间需要 14 分钟。

  相信在学习JAVA时大家估计对线程都有很深的印象吧,如今当我开始接触Android开发时,真真正正的发现了线程是多麽的重要,现在就把我对线程的理解分享给大家。

  大家一定要分清线程进程不是一回事,进程是什么呢?进程就如我们需要执行class文件,而线程才是真正调用CPU资源来运行的。一个class文件一般只有一个进程,但线程可以有很多个,线程的执行时一种异步的执行方式。

  不再扯这些没用的了,直奔主题哈。

一、如何在main函数中再开启一个线程:

public class Thread_one {    public static void main(String [] args){        Run run = new Run();        //run.run();//此为方法的调用,和线程有着天壤之别        Thread thread = new Thread(run);        thread.start();//启动线程,调用线程的run()方法        for(int i=1; i<=20; i++){            System.out.println("主线程i的 值:--------"+i);        }    }}class Run implements Runnable{    @Override    public void run() {        for(int i=1; i<=20; i++){            System.out.println("子线程i的 值:"+i);        }    }}

二、线程中的sleep方法

public class Thread_sleep {    /*     * 线程停顿     */    public static void main(String [] args){        Runone run = new Runone();        Thread thread = new Thread(run);         thread.start();        try {            Thread.sleep(5000);            thread.interrupt();//中断线程的执行            //thread.stop();//相对中断线程,stop过于粗暴,不建议使用,一般在需要强制关闭子线程时方使用此方法        } catch (InterruptedException e) {            e.printStackTrace();        }    }}class Runone implements Runnable{    @Override    public void run() {        for(int i=1 ; i<10; i++){            try {                Thread.sleep(1000);                System.out.println("-----"+new Date()+"-----");            } catch (InterruptedException e) {                return ;//当捕获到子线程被中断时,直接关闭子线程            }                    }    }}

在这里特别说明一点,thread.interrupt();可以中断线程的执行,相对stop温柔那么一点点,不过还不是最佳的关闭线程的方法,下面就为大家提供一种方式:

public class Thread_stop {    public static void main(String [] args){        Runthree run = new Runthree();        Thread th = new Thread(run);        th.start();        try {            Thread.sleep(5000);        } catch (InterruptedException e) {            e.printStackTrace();        }        run.setStop();    }}class Runthree implements Runnable{    boolean flag;    @Override    public void run() {        flag = true;        int i = 0;        while(flag){            try {                System.out.println("子线程----"+(i++));                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }    public void setStop(){        flag = false;    }    }

下面就简单给大家介绍一下线程中的合并和让出:

一、如何合并线程,此处调用了join()方法

public class Thread_join {    /*     * 合并线程     */    public static void main(String [] args){        Runtwo run = new Runtwo();        Thread thread = new Thread(run);        thread.start();        try {            thread.join();//合并线程,此时相当于方法调用        } catch (InterruptedException e) {            e.printStackTrace();        }        for(int i=0; i<10; i++){            System.out.println("主线程:"+i);        }    }}class Runtwo implements Runnable{    @Override    public void run() {        for(int i=0; i<10; i++){            System.out.println("子线程:----"+i);        }    }    }

二、如何让出线程,此处调用了Thread的yield()方法:

public class Thread_yield {    /**让出CPU     * @param args     */    public static void main(String[] args) {        Th th = new Th("aaa");        th.start();        for(int i = 0 ; i<=10; i++){            System.out.println("主线程----"+i);        }    }}class Th extends Thread{    Th(){}    Th(String s){super(s);}    @Override    public void run() {        for(int i = 0; i<=10; i++){            if(i%3!=0){                System.out.println("子线程"+i);            }else{                System.out.println("子线程i="+i+" 线程进行切换");                yield();//从Thread继承方可使用此方法            }        }    }}

最后和大家分享一下关于线程的优先级的问题:

public class Thread_priority {    /*     * priority设置线程的优先级     * Thread默认的优先级为5;Thread的最大优先级为10,最小为0     */    public static void main(String [] args){        T1 t1 = new T1();        T2 t2 = new    T2();        t1.start();            //t1.setPriority(Thread.NORM_PRIORITY+3);//设置t1的优先级        t2.start();    }}class T1 extends Thread{    @Override    public void run() {        for(int i = 0; i<50; i++){            System.out.println("线程T1-----"+i);        }    }}class T2 extends Thread{    @Override    public void run() {        for(int i = 0; i<50; i++){            System.out.println("线程T2"+i);        }    }    }

  相信大家通过以上代码基本已经了解JAVA中的线程机制,对于线程的同步,我会在下一篇文章中和大家进行交流,如有什么疑问欢迎骚扰。

  最后声明一下,以上内容如有错误,还望指点。

转载于:https://www.cnblogs.com/AndroidJotting/p/3936292.html

你可能感兴趣的文章
javaweb 导出文件名乱码的问题解决方案
查看>>
【转】Linux/ubuntu下apache+svn安装配置
查看>>
uva 10716 Evil Straw Warts Live
查看>>
230. Kth Smallest Element in a BST
查看>>
WPF中Grid实现网格,表格样式通用类(转)
查看>>
CSS中position的absolute和relative用法
查看>>
JAVA多线程
查看>>
ACE(Adaptive Communication Environment)介绍
查看>>
delphi 更改DBGrid 颜色技巧
查看>>
python编码问题
查看>>
POJ 2031 Building a Space Station
查看>>
面向对象1
查看>>
编程开发之--java多线程学习总结(5)
查看>>
register_globals(全局变量注册开关)
查看>>
[转载] 晓说——第9期:多如牛毛严酷无比的美国那些法
查看>>
[转载] New Concept English 1——Lesson 7 Are you a teacher?
查看>>
as3调用外部swf里的类的方法
查看>>
如何让 zend studio 10 识别 Phalcon语法并且进行语法提示
查看>>
任意阶幻方(魔方矩阵)C语言实现
查看>>
视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
查看>>