您好,欢迎访问一九零五行业门户网

Java并发之线程的使用以及构建启动线程

构建线程thread说明线程是程序中的执行线程,java虚拟机允许应用程序并发的运行多个线程。
每个线程都有一个优先级,高优先级线程的执行优先于低优先级线程。每个线程都可以或不可以标记为一个守护程序。当某个线程中运行的代码创建一个新 thread 对象时,该新线程的初始优先级被设定为创建线程的优先级,并且当且仅当创建线程是守护线程时,新线程才是守护程序。
当 java 虚拟机启动时,通常都会有单个非守护线程(它通常会调用某个指定类的 main 方法)。java 虚拟机会继续执行线程,直到下列任一情况出现时为止:  
           1.调用了 runtime 类的 exit 方法,并且安全管理器允许退出操作发生。  
           2.非守护线程的所有线程都已停止运行,无论是通过从对 run 方法的调用中返回,还是通过抛出一个传播到 run 方法之外的异常。
实现线程的方式,会在后续的章节中介绍
源码参考如下:
/** * a <i>thread</i> is a thread of execution in a program. the java * virtual machine allows an application to have multiple threads of * execution running concurrently. * <p> * every thread has a priority. threads with higher priority are * executed in preference to threads with lower priority. each thread * may or may not also be marked as a daemon. when code running in * some thread creates a new <code>thread</code> object, the new * thread has its priority initially set equal to the priority of the * creating thread, and is a daemon thread if and only if the * creating thread is a daemon. * <p> * when a java virtual machine starts up, there is usually a single * non-daemon thread (which typically calls the method named * <code>main</code> of some designated class). the java virtual * machine continues to execute threads until either of the following * occurs: * <ul> * <li>the <code>exit</code> method of class <code>runtime</code> has been * called and the security manager has permitted the exit operation * to take place. * <li>all threads that are not daemon threads have died, either by * returning from the call to the <code>run</code> method or by * throwing an exception that propagates beyond the <code>run</code> * method. * </ul> * <p> * there are two ways to create a new thread of execution. one is to * declare a class to be a subclass of <code>thread</code>. this * subclass should override the <code>run</code> method of class * <code>thread</code>. an instance of the subclass can then be * allocated and started. for example, a thread that computes primes * larger than a stated value could be written as follows: * <hr><blockquote><pre> * class primethread extends thread { * long minprime; * primethread(long minprime) { * this.minprime = minprime; * } * * public void run() { * // compute primes larger than minprime * &nbsp;.&nbsp;.&nbsp;. * } * } * </pre></blockquote><hr> * <p> * the following code would then create a thread and start it running: * <blockquote><pre> * primethread p = new primethread(143); * p.start(); * </pre></blockquote> * <p> * the other way to create a thread is to declare a class that * implements the <code>runnable</code> interface. that class then * implements the <code>run</code> method. an instance of the class can * then be allocated, passed as an argument when creating * <code>thread</code>, and started. the same example in this other * style looks like the following: * <hr><blockquote><pre> * class primerun implements runnable { * long minprime; * primerun(long minprime) { * this.minprime = minprime; * } * * public void run() { * // compute primes larger than minprime * &nbsp;.&nbsp;.&nbsp;. * } * } * </pre></blockquote><hr> * <p> * the following code would then create a thread and start it running: * <blockquote><pre> * primerun p = new primerun(143); * new thread(p).start(); * </pre></blockquote> * <p> * every thread has a name for identification purposes. more than * one thread may have the same name. if a name is not specified when * a thread is created, a new name is generated for it. * <p> * unless otherwise noted, passing a {@code null} argument to a constructor * or method in this class will cause a {@link nullpointerexception} to be * thrown. * * @author unascribed * @see runnable * @see runtime#exit(int) * @see #run() * @see #stop() * @since jdk1.0 */ publicclass thread implements runnable {
需要的信息 在运行线程之前首先要构造一个线程对象,线程对象在构造的时候需要提供线程所需要的属性,如线程所属的线程组、线程优先级、是否是daemon线程等信息。在new thread时会调用以下方法进行实例化thread对象。
初始化代码如下:
/** * initializes a thread. * * @param g the thread group * @param target the object whose run() method gets called * @param name the name of the new thread * @param stacksize the desired stack size for the new thread, or * zero to indicate that this parameter is to be ignored. * @param acc the accesscontrolcontext to inherit, or * accesscontroller.getcontext() if null */ private void init(threadgroup g, runnable target, string name, long stacksize, accesscontrolcontext acc) { if (name == null) { throw new nullpointerexception("name cannot be null"); } this.name = name; //当前线程作为该线程的父线程 thread parent = currentthread(); securitymanager security = system.getsecuritymanager(); //线程组的获取:如果传入的参数为空首先获取系统默认的安全组,如果为空获取父线程的安全组 if (g == null) { /* determine if it's an applet or not */ /* if there is a security manager, ask the security manager what to do. */ if (security != null) { g = security.getthreadgroup(); } /* if the security doesn't have a strong opinion of the matter use the parent thread group. */ if (g == null) { g = parent.getthreadgroup(); } } /* checkaccess regardless of whether or not threadgroup is explicitly passed in. */ g.checkaccess(); /* * do we have the required permissions? */ if (security != null) { if (isccloverridden(getclass())) { security.checkpermission(subclass_implementation_permission); } } g.addunstarted(); this.group = g; //设置daemon 、priority 属性为父线程对应的值 this.daemon = parent.isdaemon(); this.priority = parent.getpriority(); if (security == null || isccloverridden(parent.getclass())) this.contextclassloader = parent.getcontextclassloader(); else this.contextclassloader = parent.contextclassloader; this.inheritedaccesscontrolcontext = acc != null ? acc : accesscontroller.getcontext(); this.target = target; setpriority(priority); //将父线程的inheritablethreadlocal复制过来 if (parent.inheritablethreadlocals != null) this.inheritablethreadlocals = threadlocal.createinheritedmap(parent.inheritablethreadlocals); /* stash the specified stack size in case the vm cares */ this.stacksize = stacksize; /* set thread id */ //生成线程id(一个long型的字段threadseqnumber) tid = nextthreadid(); }
结论 一个新构建的thread对象(new thread()),是由其父线程(当前线程)进行空间分配,而子线程继承了父线程的daemon、优先级和加载资源的contextclassloader,以及可继承的threadlocal,同时会为子线程分配一个线程id。一个可以运行的线程对象完成初始化工作,并且在堆内存中等待运行。
构建的方式继承thread代码//方法1通过继承thread实现class mythread extends thread{ //需要实现的方法,该方法执行具体的业务逻辑 @override public void run() { system.out.println(thread.currentthread().getname() +" @@@@ mythread。run()我是通过继承thread实现的多线程"); } }
说明 通过thread源码发现(thread implements runnable)发现thread其实也是一个实现了runnable接口的一个实例,它代表一个线程的实例,并且,启动线程的唯一方法就是通过thread类的start()实例方法。start()方法是一个native方法,它将启动一个新线程,并执行run()方法。这种方式实现多线程很简单,通过自己的类直接extend thread,并复写run()方法,就可以启动新线程并执行自己定义的run()方法。
其中run()方法的方法体代表了线程需要完成的任务,称之为线程执行体。当创建此线程类对象时一个新的线程得以创建,并进入到线程新建状态。通过调用线程对象引用的start()方法,使得该线程进入到就绪状态,此时此线程并不一定会马上得以执行,这取决于cpu调度时机。
实现接口runnable代码//方法2通过实现runnable接口 //实现runnable接口,并重写该接口的run()方法,该run()方法同样是线程执行体,创建runnable实现类的实例, //并以此实例作为thread类的target来创建thread对象,该thread对象才是真正的线程对象。class myrunnable implements runnable{ @override public void run() { system.out.println(thread.currentthread().getname()+ " @@@@ myrunnable。run()我是通过实现runnable接口实现的多线程"); } }
使用callable、future实现有返回结果的多线程 使用callable和future接口创建线程。具体是创建callable接口的实现类,并实现clall()方法。并使用futuretask类来包装callable实现类的对象,且以此futuretask对象作为thread对象的target来创建线程。
可返回值的任务必须实现callable接口,类似的,无返回值的任务必须runnable接口。执行callable任务后,可以获取一个future的对象,在该对象上调用get就可以获取到callable任务返回的object了,再结合线程池接口executorservice就可以实现传说中有返回结果的多线程了(关于executor的使用后续的文章中详细介绍。)。
//方法3通过executor框架实现class mycallable implements callable<integer>{ //需要实现call方法而不是run方法 @override public integer call() throws exception { return 100; } }
启动线程通过源码分析得出:
1.对象初始化完成之后,通过执行start方法来执行这个线程,并且java虚拟机会调用该线程的run方法执行线程的业务逻辑;
2.调用start方法之后发现会同时有两个线程在执行:当前线程(parent线程【同步告知java虚拟机,只要线程规划器空闲,应立即启动调用start方法的线程】,从调用返回给start方法)和另一个线程(执行其run方法)。
3.并且多次启动一个线程是非法的。特别是当线程已经结束执行后,不能再重新启动。
start方法源码说明如下:
/** * causes this thread to begin execution; the java virtual machine * calls the <code>run</code> method of this thread. * <p> * the result is that two threads are running concurrently: the * current thread (which returns from the call to the * <code>start</code> method) and the other thread (which executes its * <code>run</code> method). * <p> * it is never legal to start a thread more than once. * in particular, a thread may not be restarted once it has completed * execution. * * @exception illegalthreadstateexception if the thread was already * started. * @see #run() * @see #stop() */ public synchronized void start() { /** * this method is not invoked for the main method thread or "system" * group threads created/set up by the vm. any new functionality added * to this method in the future may have to also be added to the vm. * * a zero status value corresponds to state "new". */ if (threadstatus != 0) throw new illegalthreadstateexception(); /* notify the group that this thread is about to be started * so that it can be added to the group's list of threads * and the group's unstarted count can be decremented. */ group.add(this); boolean started = false; try { start0(); started = true; } finally { try { if (!started) { group.threadstartfailed(this); } } catch (throwable ignore) { /* do nothing. if start0 threw a throwable then it will be passed up the call stack */ } } } private native void start0();
参考代码public class testcreatethread { public static void main(string[] args) { thread mythread = new mythread(); mythread.setname("mythread"); mythread.start(); runnable myrunnable = new myrunnable(); thread myrunnablethread = new thread(myrunnable); myrunnablethread.setname("myrunnablethread"); myrunnablethread.start(); thread myrunnablethread2 = new mythread(myrunnable); myrunnablethread2.setname("myrunnablethread2"); myrunnablethread2.start(); //执行结果参考如下: //mythread @@@@ mythread。run()我是通过继承thread实现的多线程 //myrunnablethread2 @@@@ mythread。run()我是通过继承thread实现的多线程 //myrunnablethread @@@@ myrunnable。run()我是通过实现runnable接口实现的多线程 //测试callable方法 // 创建mycallable对象 callable<integer> mycallable = new mycallable(); //使用futuretask来包装mycallable对象 futuretask<integer> ft = new futuretask<integer>(mycallable); //futuretask对象作为thread对象的target创建新的线程 thread thread = new thread(ft); thread.start();//启用 //获取信息 try { //取得新创建的新线程中的call()方法返回的结果 //当子线程此方法还未执行完毕,ft.get()方法会一直阻塞, //直到call()方法执行完毕才能取到返回值。 int sum = ft.get(); system.out.println("sum = " + sum); } catch (interruptedexception e) { e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); } //使用executorservice处理多线程 executorservice pool = executors.newfixedthreadpool(10); future<integer> f = pool.submit(mycallable); // 关闭线程池 pool.shutdown(); try { int sum1 = f.get(); system.out.println("sum1 = " + sum1); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); } } }
启动线程的注意事项
无论何种方式,启动一个线程,就要给它一个名字!这对排错诊断系统监控有帮助。否则诊断问题时,无法直观知道某个线程的用途。
thread与runnable的关系实现关系thread实现接口runnable,并且实现了run方法,代码参考如下:
//如果该线程是使用独立的 runnable 运行对象构造的,则调用该 runnable 对象的 run 方法; //否则,该方法不执行任何操作并返回。 //thread 的子类应该重写该方法。 /** * if this thread was constructed using a separate * <code>runnable</code> run object, then that * <code>runnable</code> object's <code>run</code> method is called; * otherwise, this method does nothing and returns. * <p> * subclasses of <code>thread</code> should override this method. * * @see #start() * @see #stop() * @see #thread(threadgroup, runnable, string) */ @override public void run() { if (target != null) { target.run(); } } }
区别      当执行到thread类中的run()方法时,会首先判断target是否存在,存在则执行target中的run()方法,也就是实现了runnable接口并重写了run()方法的类中的run()方法。当时如果该runnable的子类是通过一个继承thread的子类(该且重写了run方法),则真正执行的是thread子类重写的run方法(由于多态的原因)。
以上就是java并发之线程的使用以及构建启动线程的详细内容。
其它类似信息

推荐信息