栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA线程用法详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA线程用法详解

本文配合实例较为详细的讲解了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 {

  
  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 {
  
  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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/152067.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号