Java多线程
Posted mrzhang1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java多线程相关的知识,希望对你有一定的参考价值。
Java中的多线程概述
Java中的线程都是通过Thread类来管理的。Thread类中维护一个target,该target就是用户要创建的任务对象。所以要创建多任务必须实现Runnable接口。当然继承Thread类也可以实现多任务。
private Runnable target;
private void init(ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc) { ...................... g.addUnstarted(); this.group = g; 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; //target 也就是runnable接口类型的对象。 setPriority(priority); 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 */ tid = nextThreadID(); }
第一种方式:
实现runnable接口,并实现run()方法。 该方式任务没有返回值
class demo1 implements Runnable { @Override public void run() { while( true ){ try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } show1(); } } private synchronized void show1() { System.out.println("demo1_show1"); } } class test{ public static void main(String[] args) { Thread t1 = new Thread(new demo1()); t1.start(); } }
第二种方式:
实现Callable<V>接口,FutureTask<V>实现了Runnable接口的run()方法,该方法中调用的Callbale<V>中的call()方法,并保存了返回值。所以这里的实现方式是多封装了一层。
public class CallableDemo implements Callable<Integer>{ @Override public Integer call() throws Exception { // TODO Auto-generated method stub System.out.println("call"); Thread.sleep(100); return 1; } @Test public void init( ) throws InterruptedException, ExecutionException{ FutureTask<Integer> futureTask = new FutureTask<>(new CallableDemo()); new Thread(futureTask).start(); System.out.println("taskRet : "+futureTask.get()); } }
因为最后都是通过Thread类管理的线程。而Callable没有继承Runnable接口。但是FutureTask<V>实现了Runable接口,并实现了run()方法。FutureTask<V>的run()方法源码如下:
public void run() { if (state != NEW || !UNSAFE.compareAndSwapObject(this, runnerOffset, null, Thread.currentThread())) return; try { Callable<V> c = callable; if (c != null && state == NEW) { V result; boolean ran; try { result = c.call(); //获取call()函数的返回值。 ran = true; } catch (Throwable ex) { result = null; ran = false; setException(ex); } if (ran) set(result); //保存返回值,通过get()方法获取该返回值。 } } finally { // runner must be non-null until state is settled to // prevent concurrent calls to run() runner = null; // state must be re-read after nulling runner to prevent // leaked interrupts int s = state; if (s >= INTERRUPTING) handlePossibleCancellationInterrupt(s); } }
以上是关于Java多线程的主要内容,如果未能解决你的问题,请参考以下文章