当前位置: 移动技术网 > IT编程>开发语言>Java > java--多线程编程简介

java--多线程编程简介

2018年09月27日  | 移动技术网IT编程  | 我要评论

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{

    @override
    public 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[daemonrunner,5,main]
进入守护线程,说明现在还有其他线程在执行
我是其他线程

首先,先启动其他线程,需要耗时1500ms,同时,主线程耗时1000ms后,开始进入守护线程,此时其它线程还在运行,到了守护线程,耗时300ms,其他线程仍在执行,继续往下,守护线程执行完毕

但是如果我把守护线程的300ms改成500ms,会发生什么事呢?

出现过两种情况,毕竟在临界值

1.我是其他线程

2.thread[daemonrunner,5,main]
进入守护线程,说明现在还有其他线程在执行
我是其他线程

 

 

本文来自 zejian_ 的csdn 博客 ,全文地址请点击:https://blog.csdn.net/javazejian/article/details/50878598?utm_source=copy 

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

相关文章:

验证码:
移动技术网