如何查看cpu的个数:资源管理器
设备管理器方式
java查看处理器个数
@Test
public void testCpuNum(){Runtime runtime = Runtime.getRuntime();int i = runtime.availableProcessors();System.out.println("处理器个数为:"+i);
}
多线程图示
java中jconsole的使用:
控制台输入
找到对应的进程
package com.hspedu.threaduse;/*** 演示通过继承Thread 类创建线程*/
public class Thread01 {public static void main(String[] args) throws InterruptedException {//创建Cat对象,可以当做线程使用Cat cat = new Cat();//读源码/*(1)public synchronized void start() {start0();}(2)//start0() 是本地方法,是JVM调用, 底层是c/c++实现//真正实现多线程的效果, 是start0(), 而不是 runprivate native void start0();*/cat.start();//启动线程-> 最终会执行cat的run方法//cat.run();//run方法就是一个普通的方法, 没有真正的启动一个线程,就会把run方法执行完毕,才向下执行//说明: 当main线程启动一个子线程 Thread-0, 主线程不会阻塞, 会继续执行//这时 主线程和子线程是交替执行..System.out.println("主线程继续执行" + Thread.currentThread().getName());//名字mainfor(int i = 0; i < 60; i++) {System.out.println("主线程 i=" + i);//让主线程休眠Thread.sleep(1000);}}
}//说明
//1. 当一个类继承了 Thread 类, 该类就可以当做线程使用
//2. 我们会重写 run方法,写上自己的业务代码
//3. run Thread 类 实现了 Runnable 接口的run方法
/*@Overridepublic void run() {if (target != null) {target.run();}}*/class Cat extends Thread {int times = 0;@Overridepublic void run() {//重写run方法,写上自己的业务逻辑while (true) {//该线程每隔1秒。在控制台输出 “喵喵, 我是小猫咪”System.out.println("喵喵, 我是小猫咪" + (++times) + " 线程名=" + Thread.currentThread().getName());//让该线程休眠1秒 ctrl+alt+ttry {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}if(times == 80) {break;//当times 到80, 退出while, 这时线程也就退出..}}}
}
1.start0() 是本地方法,是JVM调用, 底层是c/c++实现
2.真正实现多线程的效果, 是start0(), 而不是 run
private native void start0();
3.当一个类继承了 Thread 类, 该类就可以当做线程使用
4.我们会重写 run方法,写上自己的业务代码
5.run Thread 类 实现了 Runnable 接口的run方法
package com.hspedu.threaduse;/**
* @version 1.0
* 通过实现接口Runnable 来开发线程
*/
public class Thread02 {public static void main(String[] args) {Dog dog = new Dog();//dog.start(); 这里不能调用start//创建了Thread对象,把 dog对象(实现Runnable),放入ThreadThread thread = new Thread(dog);thread.start();// Tiger tiger = new Tiger();//实现了 Runnable
// ThreadProxy threadProxy = new ThreadProxy(tiger);
// threadProxy.start();}
}class Animal {
}class Tiger extends Animal implements Runnable {@Overridepublic void run() {System.out.println("老虎嗷嗷叫....");}
}//线程代理类 , 模拟了一个极简的Thread类
class ThreadProxy implements Runnable {//你可以把Proxy类当做 ThreadProxyprivate Runnable target = null;//属性,类型是 Runnable@Overridepublic void run() {if (target != null) {target.run();//动态绑定(运行类型Tiger)}}public ThreadProxy(Runnable target) {this.target = target;}public void start() {start0();//这个方法时真正实现多线程方法}public void start0() {run();}
}class Dog implements Runnable { //通过实现Runnable接口,开发线程int count = 0;@Overridepublic void run() { //普通方法while (true) {System.out.println("小狗汪汪叫..hi" + (++count) + Thread.currentThread().getName());//休眠1秒try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}if (count == 10) {break;}}}
}
代理模式,有静态代理和动态代理
通过Thread含参构造实现的线程创建底层实际运用了代理模式
/*** main线程启动两个子线程*/
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();//启动第1个线程thread2.start();//启动第2个线程//...}
}class T1 implements Runnable {int count = 0;@Overridepublic void run() {while (true) {//每隔1秒输出 “hello,world”,输出10次System.out.println("hello,world " + (++count));try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}if(count == 60) {break;}}}
}class T2 implements Runnable {int count = 0;@Overridepublic void run() {//每隔1秒输出 “hi”,输出5次while (true) {System.out.println("hi " + (++count));try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}if(count == 50) {break;}}}
}
自我思考:
多线程是什么,为什么要使用多线程,有什么好处
首先多线程可以理解为:假设一个程序有多个功能,如果只有一个主线程(一个人)执行,那么从头到尾都是一个线程(人)在干活,现在有了多线程,我可以将一个程序交给不同的线程(人)去完成对应的功能,比如A线程负责下载,B线程负责上传,这个操作是可以同时进行的,大大提升了程序的效率
package com.hspedu.ticket;/*** 使用多线程,模拟三个窗口同时售票100张*/
public class SellTicket {public static void main(String[] args) {//测试
// SellTicket01 sellTicket01 = new SellTicket01();
// SellTicket01 sellTicket02 = new SellTicket01();
// SellTicket01 sellTicket03 = new SellTicket01();
//
// //这里我们会出现超卖..
// sellTicket01.start();//启动售票线程
// sellTicket02.start();//启动售票线程
// sellTicket03.start();//启动售票线程System.out.println("===使用实现接口方式来售票=====");SellTicket02 sellTicket02 = new SellTicket02();new Thread(sellTicket02).start();//第1个线程-窗口new Thread(sellTicket02).start();//第2个线程-窗口new Thread(sellTicket02).start();//第3个线程-窗口}
}//使用Thread方式
class SellTicket01 extends Thread {private static int ticketNum = 100;//让多个线程共享 ticketNum@Overridepublic void run() {while (true) {if (ticketNum <= 0) {System.out.println("售票结束...");break;}//休眠50毫秒, 模拟try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("窗口 " + Thread.currentThread().getName() + " 售出一张票"+ " 剩余票数=" + (--ticketNum));}}
}//实现接口方式
class SellTicket02 implements Runnable {private int ticketNum = 100;//让多个线程共享 ticketNum@Overridepublic void run() {while (true) {if (ticketNum <= 0) {System.out.println("售票结束...");break;}//休眠50毫秒, 模拟try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("窗口 " + Thread.currentThread().getName() + " 售出一张票"+ " 剩余票数=" + (--ticketNum));//1 - 0 - -1 - -2}}
}