湛蓝之海 发表于 2021-10-8 13:17:32

实例总结Java多线程编程的方法

在本篇文章里我们给大家总结了Java多线程编程的方法以及相关实例代码,需要的朋友们可以学习下。
1.什么时候使用多线程编程
一个任务在正常情况下是按顺序执行的,但是如果当前任务里有多个相似进程块(例如for,while语句),我们就可以考虑把这些代码块抽出来并行运行,无需阻塞
2.实现多线程的几种方式
一种是继承thread类重写run方法,另一种是实现runnable接口重写run方法
启动多线程很多情况下是为了处理并发进程,此时对于部分实时性要求不是那么高的业务需求,我们还可以通过实现队列的方式,异步实现。
3.举例
继承thread


/**
*
* @classname: threadbyex
* @description: todo
* @author mr.jqcheng
* @date 2018年9月26日
* */public class threadbyex extends thread{

@overridepublic void run() {    // todo auto-generated method stub
    system.out.println("我是继承线程");
}

}
实现runnable


/**
*
* @classname: threadbyrunnable
* @description: todo
* @author mr.jqcheng
* @date 2018年9月26日
* */public class threadbyrunnable implements runnable{/*public threadbyrunnable() {
    this.run();
    // todo auto-generated constructor stub
}*/

public void run() {    // todo auto-generated method stub
    system.out.println("我是实现进程");
}

}
测试:


/**
*
* @classname: test
* @description: todo
* @author mr.jqcheng
* @date 2018年9月26日
* */public class test {public static void main(string[] args) {    // 继承thread启动的方法
    threadbyex t1 = new threadbyex();
    t1.start();// 启动线程    // 实现runnable启动线程的方法
    threadbyrunnable r = new threadbyrunnable();
    thread t2 = new thread(r);
    t2.start();// 启动线程    //new threadbyrunnable();}

}
运行结果:
我是继承线程
我是实现进程
ok,简单的多线程实现方式完成了,在调用start()的时候,该进程已经进入可执行状态,等待系统执行。
线程处理的几个常用方法:
void interrupt():向线程发送中断请求,线程的中断状态将会被设置为true,如果当前线程被一个sleep调用阻塞,那么将会抛出interrupedexception异常。
static boolean interrupted():测试当前线程(当前正在执行命令的这个线程)是否被中断。注意这是个静态方法,调用这个方法会产生一个副作用那就是它会将当前线程的中断状态重置为false。
boolean isinterrupted():判断线程是否被中断,这个方法的调用不会产生副作用即不改变线程的当前中断状态。
static thread currentthread() : 返回代表当前执行线程的thread对象。
守护进程
用来服务于不是服务进程的其他所有当前进程下的所有线程
实现deamon.setdaemon(true)就行,要在线程开启之前启用
举例


package com.orange.util;
/**
*
* @classname: test
* @description: todo
* @author mr.jqcheng
* @date 2018年9月26日
*
*/
public class test {
public static void main(string[] args) {
    thread deamon2 = new thread(new daemonrunner2(), "otherrunner");
    deamon2.start();// 启动线程
    try {
      thread.sleep(1000);
    } catch (interruptedexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
    }
    thread deamon = new thread(new daemonrunner(), "daemonrunner");
    // 设置为守护线程
    deamon.setdaemon(true);
    deamon.start();// 启动线程
}
static class daemonrunner implements runnable {
    public void run() {
      // todo auto-generated method stub
      try {
      thread.sleep(300);
      thread t = thread.currentthread();
      system.out.println(t);
      } catch (exception e) {
      e.printstacktrace();
      } finally {
      system.out.println("进入守护线程,说明现在还有其他线程在执行");
      }
    }
}
static class daemonrunner2 implements runnable {
    public void run() {
      // todo auto-generated method stub
      try {
      thread.sleep(1500);
      system.out.println("我是其他线程");
      } catch (exception e) {
      e.printstacktrace();
      }
    }
}
}
执行结果:
thread
进入守护线程,说明现在还有其他线程在执行
我是其他线程
首先,先启动其他线程,需要耗时1500ms,同时,主线程耗时1000ms后,开始进入守护线程,此时其它线程还在运行,到了守护线程,耗时300ms,其他线程仍在执行,继续往下,守护线程执行完毕
但是如果我把守护线程的300ms改成500ms,会发生什么事呢?
出现过两种情况,毕竟在临界值
1.我是其他线程
2.thread
进入守护线程,说明现在还有其他线程在执行
我是其他线程

http://www.zzvips.com/article/168287.html
页: [1]
查看完整版本: 实例总结Java多线程编程的方法