标签:
java创建线程两种方式:
1.继承Thread创建线程
/** * Created by lsf on 16/4/18. */ class NewThread extends Thread { NewThread(){ super(); //创建线程 start(); //启动线程 } public void run() { long starttime = System.currentTimeMillis(); System.out.println("child thread..."+starttime); } } class CurrentThreadDemo { public static void main(String args[]) { long starttime2 = System.currentTimeMillis(); System.out.println("main thread,,,"+starttime2); //主线程 new NewThread(); System.out.println("住县城"); } }
2.实现
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion; import java.util.Date; /** * Created by lsf on 16/4/18. */ class NewThread extends Thread { NewThread(){ Thread t = new Thread(this); //创建线程 t.start(); //启动线程 } public void run() { long starttime = System.currentTimeMillis(); System.out.println("child thread..."+starttime); } } class CurrentThreadDemo { public static void main(String args[]) { long starttime2 = System.currentTimeMillis(); System.out.println("main thread,,,"+starttime2); new NewThread(); System.out.println("主线程"); //主线程 } }
3.给任务创建多个线程去执行
class NewThread extends Thread { String name; NewThread(String threadname){ name = threadname; Thread t = new Thread(this,threadname); //创建线程 t.start(); //启动线程 } public void run() { long starttime = System.currentTimeMillis(); System.out.println("child thread..."+name); } } class CurrentThreadDemo { public static void main(String args[]) { long starttime2 = System.currentTimeMillis(); System.out.println("main thread,,,"+starttime2); new NewThread("demo1"); new NewThread("demo2"); new NewThread("demo3"); System.out.println("主线程"); //主线程 } }
4.线程优先级设置
/** * Created by lsf on 16/4/22. */ class ThreadTest implements Runnable { Thread t; int count = 0; private volatile Boolean flag = true; public ThreadTest(int p) { t = new Thread(this); t.setPriority(p); } public void start(){ t.start(); } public void finish(){ flag = false; } @Override public void run() { while(flag){ count++; } } } public class ThreadPriority { public static void main(String[] args) { ThreadTest t1 = new ThreadTest(Thread.NORM_PRIORITY - 2); ThreadTest t2 = new ThreadTest(Thread.NORM_PRIORITY + 2); t1.start(); t2.start(); t1.finish(); t2.finish(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } try { System.out.println("t1 count:"+t1.count); System.out.println("t2 count:"+t2.count); t1.t.join(); t2.t.join(); System.out.println("t1 is alive:" + t1.t.isAlive()); System.out.println("t2 is alive:" + t1.t.isAlive()); } catch (InterruptedException e) { e.printStackTrace(); } } }
5.线程同步
线程同步的关键在于同一时刻线程在管程内,应用场景一般是:当某个方法(callme)需要用多线程去执行,可以改造一下对应的方法,加上关键词synchronized,这样在调用过程中,每个线程都会默认进入隐式管程。
/** * Created by root on 16-4-15. */ class Callme { synchronized void call(String msg){ System.out.println("["+msg); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("]"); } } class ThreadCaller implements Runnable{ String msg; Callme target; Thread t; public ThreadCaller(Callme targ,String s) { target = targ; msg = s; t = new Thread(this); t.start(); } @Override public void run() { target.call(msg); } } class Demo { public static void main(String[] args) { Callme target = new Callme(); ThreadCaller obj1 = new ThreadCaller(target,"Hello"); ThreadCaller obj2 = new ThreadCaller(target,"Synchronized"); ThreadCaller obj3 = new ThreadCaller(target,"World"); try { obj1.t.join(); obj2.t.join(); obj3.t.join(); } catch (InterruptedException e) { System.out.println("Interrupted"); } } }
标签:
原文地址:http://www.cnblogs.com/alexkn/p/5418084.html