欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java的多线程开发(一)

程序员文章站 2022-06-29 13:50:27
java 启动新线程有三种方法: 类 Thread 直接new 接口 Runnable 接口 callable Callable 可以返回值 即可return 线程的结束: 早期版本 用 stop(),resume(),suspend() 其中 stop() 不能释放资源 ,suspend()容易死 ......

java 启动新线程有三种方法:

类  thread  直接new

接口  runnable

接口 callable

runnable 和callable的区别:

callable 可以返回值 即可return 

 

package com.jwz.test;

import java.util.concurrent.callable;
import java.util.concurrent.executionexception;
import java.util.concurrent.futuretask;

public class testthread {
    private static class userrun implements runnable {
        @override
        public void  run(){
            system.out.println("i am runable ");
        }
    }

    private static class usercall implements callable{
        @override
        public string call(){
            system.out.println("i am callable");
            return  "callresult";
        }
    }

    public static void main(string[] args) throws executionexception, interruptedexception {
        userrun run = new userrun();
        new thread(run).start();
        usercall call=new usercall();
        //用futuretask 构造callable对象
        futuretask<string> future=new futuretask<string>(call);
        new thread(future).start();
        system.out.println(future.get());
    }
}

  

线程的结束:

 

早期版本 用 stop(),resume(),suspend() 其中 stop() 不能释放资源 ,suspend()容易死锁。所以都不建议使用了

 

现在使用  interrput() 、 isinterrupted()、static 方法 interrputed()

interrput 方法用于中断一个线程(java 线程是协作式的),并不是强行关闭一个线程,只是跟这个线程打个招呼把中断线程的标志位设置为true ,是否重点又线程自己决定

isinterrupted 判断当前线程是否处于中断状态

static 方法 interrupted 判断当前线程是否处于中断状态,并把线程中断标志位设置为false

 

package com.jwz.thread;

public class endthread {
    private static class usethread extends thread{
        public usethread(string name){
            super(name);
        }

        @override
        public void run(){
            string threadname=thread.currentthread().getname();
            while (!isinterrupted()){
                system.out.println(threadname+" is run");
            }
            system.out.println(threadname+" is finished and isinterrupt flag is "+isinterrupted());
        }

        public static void main(string[] args) throws interruptedexception {
            thread usethread=new usethread("endthread");
            usethread.start();
            sleep(20);
            usethread.interrupt();
        }
    }
}

  

线程里没有判断isinterrupted()的判断线程就不会中断

 

如果run() 方法 里有thread.slee()方法 用try catch 抓取异常的时候 需要在 catch中调用 interrupt()方法才能中断线程

 

public class endthreadrun {
    private static class userun implements runnable{
        @override
        public void run(){
            string threadname=thread.currentthread().getname();
            while(!thread.currentthread().isinterrupted()){

                try {
                    thread.sleep(100);
                } catch (interruptedexception e) {
                    system.out.println(threadname+" is strop and interrupt is "
                            +thread.currentthread().isinterrupted());
                    e.printstacktrace();
                    thread.currentthread().interrupt();
                    system.out.println(threadname);
                }
                system.out.println(threadname+" is strop and interrupt is "
                        +thread.currentthread().isinterrupted());
            }
        }
    }

    public static void main(string[] args) throws interruptedexception {
        userun userun=new userun();
        thread endthread=new thread(userun,"endthread");
        endthread.start();
        thread.sleep(20);
        endthread.interrupt();
    }
}