1、并发基础并发
什么是线程 线程的基本操做 守护线程 线程优先级 基本的线程同步操做
什么是线程线程
线程的基本操做3d
线程中断code
public static native void sleep(long millis) throws InterruptedException public void run(){ while(true){ } } if(Thread.currentThread().isInterrupted()){ System.out.println("Interruted!"); break; } try { Thread.sleep(2000); } catch (InterruptedException e) { System.out.println("Interruted When Sleep"); //设置中断状态,抛出异常后会清除中断标记位 Thread.currentThread().interrupt(); } Thread.yield();
守护线程blog
线程优先级同步
线程同步it
public class ThreadTest { public static Object object = new Object(); public static class T2 extends Thread { public void run() { synchronized (object) { System.out.println(System.currentTimeMillis() + ":T2 start! notify one thread"); object.notify(); System.out.println(System.currentTimeMillis() + ":T2 end!"); try { Thread.sleep(2000); } catch (InterruptedException e) { } } } } public static class T1 extends Thread { public void run() { synchronized (object) { System.out.println(System.currentTimeMillis() + ":T1 start! "); try { System.out.println(System.currentTimeMillis() + ":T1 wait for object "); object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(System.currentTimeMillis() + ":T1 end!"); } } } public static void main(String[] args) { T1 t1 = new T1(); t1.start(); T2 t2 = new T2(); t2.start(); } }