CPU核心获取
package chapter20; public class CpuNum { public static void main(String[] args) { Runtime runtime = Runtime.getRuntime(); int cpuNums = runtime.availableProcessors();//获取当前电脑CPU核心数量 System.out.println(cpuNums); } }
线程使用
package chapter20.Threaduse; public class Thread01 { public static void main(String[] args) throws InterruptedException { Cat cat = new Cat(); cat.start();//启动子线程,一边main输出一边run 原码 真正实现多线程的效果是 JVM调用的 start0() 不是run // cat.run();//run方法是普通方法,没有真正启动一个线程,执行完后才往下执行 System.out.println("主线程继续执行"+Thread.currentThread().getName());//main线程启动子线程Thread-0,主线程不会阻塞继续进行 for (int i = 0; i <5 ; i++) { System.out.println("主线程 i="+i); Thread.sleep(1000);//主线程休眠 //Terminal jconsole监控进程 } } } class Cat extends Thread{//继承Thread类后就变线程类 int times=0; @Override public void run() {//会重写run方法,写上自己业务代码 while (true){ System.out.println("我是毛毛"+(++times)+" 线程名="+Thread.currentThread().getName());//主子线程交替进行,主线程死后,子线程还在,所有进程结束后,进程结束 try { Thread.sleep(1000);//休眠1秒 } catch (InterruptedException e) { e.printStackTrace(); } if (times==80){ break; } } } }
package chapter20.Threaduse; //接口实现线程(可在继承了类以后) 进程独享 public class Thread02 { public static void main(String[] args) { Dog dog = new Dog(); //dog.start 没有start方法 Thread thread = new Thread(dog);//创建Thread对象,把对象dog(实现Runnable)放入Thread thread.start(); Tiger tiger = new Tiger(); ThreadProxy threadProxy = new ThreadProxy(tiger); threadProxy.start(); } } class Dog implements Runnable{ int count=0; @Override public void run() { while (true){ System.out.println("666"+(++count)+Thread.currentThread().getName()); try { Thread.sleep(1000);//休眠1秒 } catch (InterruptedException e) { e.printStackTrace(); } if (count==5){ break; } } } } class Animal{} class Tiger extends Animal implements Runnable{ @Override public void run() { System.out.println("eee"); } } class ThreadProxy implements Runnable{//线程代理类 静态代理 private Runnable target=null; @Override public void run() { if (target!=null){//运行类型tiger target.run(); } } public ThreadProxy(Runnable target) {//tiger实现了Runnable this.target = target; } public void start(){ start0(); } private void start0(){ run(); } }
package chapter20.Threaduse; //启动两个子线程 public class Thread03 { public static void main(String[] args) { T1 t1 = new T1(); T2 t2 = new T2(); Thread thread1 = new Thread(t1); Thread thread2 = new Thread(t2); thread1.start(); thread2.start(); } } class T1 implements Runnable{ int count=0; @Override public void run() { while (true){ System.out.println("hello"+(++count)); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if (count==10){ break; } } } } class T2 implements Runnable{ int count=0; @Override public void run() { while (true){ System.out.println("hi"+(++count)); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if (count==5){ break; } } } }
售票系统超卖现象
package chapter20.Threaduse; //三个窗口模拟同时售票100张 public class SellTicket { public static void main(String[] args) { /* Sell01 sell01 = new Sell01(); Sell01 sell02 = new Sell01(); Sell01 sell03 = new Sell01(); sell01.start(); sell02.start(); sell03.start(); 会出现超卖*/ Sell02 sell02 = new Sell02(); new Thread(sell02).start(); new Thread(sell02).start(); new Thread(sell02).start(); } } class Sell01 extends Thread{ private static int num=100;//多个线程共享num @Override public void run() { while (true){ if (num<=0){ System.out.println("售票结束..."); break; } try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("窗口"+Thread.currentThread().getName()+"售出一张,剩余"+(--num)); } } } class Sell02 implements Runnable{ private int num=100;//多个线程共享num @Override public void run() { while (true){ if (num<=0){//判断使进程都进来了,引起超卖 System.out.println("售票结束..."); break; } try { Thread.sleep(50);//休眠时间越长越好 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("窗口"+Thread.currentThread().getName()+"售出一张,剩余"+(--num)); } } }
线程退出
package chapter20.Threaduse; public class ThreadExit01 { public static void main(String[] args) { T t = new T(); t.start(); System.out.println("主线程休眠了"); //希望主线程控制T线程终止,修改loop变量 try { Thread.sleep(10*1000);//主线程休眠10秒再通知子线程退出 } catch (InterruptedException e) { e.printStackTrace(); } t.setLoop(false); } } class T extends Thread{ int n=0; private boolean loop =true; public void setLoop(boolean loop) { this.loop = loop; } @Override public void run() { while (loop){ try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("T 运行中"+(++n)); } } }
线程方法
package chapter20.ThreadMethod; public class ThreadMethod01 { public static void main(String[] args) throws InterruptedException { T t = new T(); t.setName("xx");//修改线程名 t.setPriority(Thread.MIN_PRIORITY); t.start(); //主线程打印5个hi 中断子线程休眠 for (int i = 0; i <5 ; i++) { Thread.sleep(1000);//主线程首先开始休眠 System.out.println("hi"+i); } System.out.println(t.getName()+"优先级="+t.getPriority());//1 t.interrupt();//中断T休眠 } } class T extends Thread{ @Override public void run() { while (true){ for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName()+"吃饱了"+i); } try { System.out.println(Thread.currentThread().getName()+"在休眠了"); Thread.sleep(20000); } catch (InterruptedException e) {//当线程执行到interrupt时,会catch异常,加入自己业务代码 System.out.println(Thread.currentThread().getName()+"被打断了"); } } } }
package chapter20.ThreadMethod; public class ThreadMethod02 { public static void main(String[] args) throws InterruptedException { T2 t2 = new T2(); t2.start(); for (int i = 1; i <=20 ; i++) { Thread.sleep(1000); System.out.println("主线程"+i); if (i==5){ // System.out.println("主线程让子线程"); // t2.join(); // System.out.println("子线程完毕,主线程继续"); Thread.yield(); System.out.println("主线程礼让,不一定成功"); } } } } class T2 extends Thread{ @Override public void run() { for (int i = 0; i <=20 ; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("子线程"+i); } } }
守护线程
package chapter20.ThreadMethod; public class ThreadMethod03 { public static void main(String[] args) throws InterruptedException { MyDaemonThread myDaemonThread = new MyDaemonThread(); myDaemonThread.setDaemon(true); //如希望main线程结束后,子线程自动结束,要把子线程设置为守护线程 myDaemonThread.start(); for (int i = 1; i <10; i++) { System.out.println("hhh"); Thread.sleep(1000); } } } class MyDaemonThread extends Thread{ @Override public void run() { for (;;){ try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("666"); } } }
线程插队练习
package chapter20.ThreadMethod; public class ThreadMethodExercise { public static void main(String[] args) throws InterruptedException { son son = new son(); Thread thread = new Thread(son); for (int i = 1; i <=10 ; i++) { Thread.sleep(1000); System.out.println("hi"+i); if (i==5){ thread.start();//主线程先来,5次后开始子线程 thread.join();//子线程插队 } } System.out.println("主线程结束..."); } } class son implements Runnable{ @Override public void run() { for (int i = 1; i <=10 ; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("hello"+i); } System.out.println("子线程退出..."); } }
线程状态
线程生命周期
package chapter20.State; public class ThreadState01 { public static void main(String[] args) throws InterruptedException { T t = new T(); System.out.println(t.getName()+"状态"+t.getState());//new t.start(); while (Thread.State.TERMINATED!=t.getState()){//不是终止状态(terminated) System.out.println(t.getName()+"状态"+t.getState());//(runnable) Thread.sleep(500);//主线程休眠,子线程间歇休眠(waiting) } System.out.println(t.getName()+"状态"+t.getState()); } } class T extends Thread{ @Override public void run() { while (true){ for (int i = 0; i <10 ; i++) { System.out.println("hi"+i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } break; } } }
线程锁
互斥锁
package chapter20.Syn; //三个窗口模拟同时售票100张 public class SellTicket { public static void main(String[] args) { /* Sell01 sell01 = new Sell01(); Sell01 sell02 = new Sell01(); Sell01 sell03 = new Sell01(); sell01.start(); sell02.start(); sell03.start(); 会出现超卖*/ // Sell02 sell02 = new Sell02(); // new Thread(sell02).start(); // new Thread(sell02).start(); // new Thread(sell02).start(); Sell03 sell03 = new Sell03(); new Thread(sell03).start(); new Thread(sell03).start(); new Thread(sell03).start(); } } class Sell01 extends Thread{ private static int num=100;//多个线程共享num @Override public void run() { while (true){ if (num<=0){ System.out.println("售票结束..."); break; } try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("窗口"+Thread.currentThread().getName()+"售出一张,剩余"+(--num)); } } } class Sell02 implements Runnable{ private int num=100;//多个线程共享num @Override public void run() { while (true){ if (num<=0){//判断是进程都进来了,引起超卖 System.out.println("售票结束..."); break; } try { Thread.sleep(50);//休眠时间越长越好 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("窗口"+Thread.currentThread().getName()+"售出一张,剩余"+(--num)); } } } //使用synchronize实现线程同步 class Sell03 implements Runnable{ private int num=100;//多个线程共享num private boolean loop =true; public synchronized void m(){//同步方法,同一时刻只能一个线程操作 锁在this对象 new了多个Sell03就锁不住,要让多线程锁的对象为同一个 //synchronized (this){}//同步代码块,锁在this对象 //类中 Object object=new Object() 括号内this可换object //加static后 锁在类Sell03.class上 不是this if (num<=0){//判断不会再让多个进程都进来,引起超卖 System.out.println("售票结束..."); loop=false; return; } try { Thread.sleep(50);//休眠时间越长越好 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("窗口"+Thread.currentThread().getName()+"售出一张,剩余"+(--num)); } @Override public void run() { while (loop){ m(); } } }
死锁
package chapter20.Syn; public class DeadLock01 { public static void main(String[] args) { DeadLockDemo A = new DeadLockDemo(true); A.setName("A"); DeadLockDemo B = new DeadLockDemo(false); B.setName("B"); A.start(); B.start(); } } class DeadLockDemo extends Thread{ static Object o1=new Object(); static Object o2=new Object(); boolean flag; public DeadLockDemo(boolean flag) { this.flag = flag; } public void run(){ if (flag){//如果为T,进程A持有o1对象锁,后去尝试获得o2对象锁,如得不到o2对象锁就会block synchronized (o1){ System.out.println(Thread.currentThread().getName()+"进入1"); synchronized (o2){ System.out.println(Thread.currentThread().getName()+"进入2"); } } } else{//如果为T,进程B持有o2对象锁,后去尝试获得o1对象锁,如得不到o1对象锁就会block synchronized (o2){ System.out.println(Thread.currentThread().getName()+"进入3"); synchronized (o1){ System.out.println(Thread.currentThread().getName()+"进入4"); } } } } }
释放锁