码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA--线程

时间:2017-04-16 22:43:15      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:主线程   关闭   文件   java   ram   rac   main函数   android   trace   

相信在学习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中的线程机制,对于线程的同步,我会在下一篇文章中和大家进行交流,如有什么疑问欢迎骚扰。

JAVA--线程

标签:主线程   关闭   文件   java   ram   rac   main函数   android   trace   

原文地址:http://www.cnblogs.com/chenshizhutou/p/6720141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!