当前位置: 移动技术网 > IT编程>开发语言>Java > JAVA线程用法详解

JAVA线程用法详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文配合实例较为详细的讲解了java的线程技术,相信对于深入理解java程序设计有一定的帮助。具体如下:

很多人在学习java时都对线程都有一定的了解,而当我们开始接触android开发时,才真真正正的发现了线程是多麽的重要,本文就把对java线程的用法心得分享给大家,供大家参考。

首先,大家一定要分清线程和进程不是一回事,进程是什么呢?进程就如我们需要执行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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网