济南网站制作价格,哪个网站可以做蛋白质的跨膜图,怎样成立网站,wordpress显示当前文章的分类文章目录 线程的介绍#xff1a;创建线程的三种方式#xff1a;一、继承Thread二、实现Runnable接口三、实现Callable接口 线程的优先级#xff1a;多线程#xff1a;线程终止#xff1a;线程常用方法#xff1a;用户线程和守护线程线程的生命周期#xff1a;Synchroniz… 文章目录 线程的介绍创建线程的三种方式一、继承Thread二、实现Runnable接口三、实现Callable接口 线程的优先级多线程线程终止线程常用方法用户线程和守护线程线程的生命周期Synchronized线程死锁释放锁的操作不会释放锁的操作 线程的介绍 1. 什么是程序2. 什么是进程 3. 什么是线程 4. 线程的相关概念 并发和并行也可以同时存在
public class CpuNum {public static void main(String[] args) {Runtime runtime Runtime.getRuntime();//获取当前电脑的cpu数量/核心数int cpuNums runtime.availableProcessors();System.out.println(当前有cpu 个数 cpuNums);}
}
创建线程的三种方式 还有一种是实现Callable接口。
一、继承Thread ** 当我们运行java程序的时候就开启了一个线程主线程main线程只有线程上所有的线程结束我们的线程才会结束** 真正实现多线程的效果 是start0(), 而不是 run
public class Thread01 {public static void main(String[] args) throws InterruptedException {//创建Cat对象可以当做线程使用Cat cat new Cat();//读源码/*(1)start方法public synchronized void start() {start0();}(2)start0方法//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. Thread 类的run 方法是 实现了 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秒 ctrlaltttry {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}if(times 80) {break;//当times 到80, 退出while, 这时线程也就退出..}}}
}
二、实现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;//属性类型是 RunnableOverridepublic 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;}}}
}三、实现Callable接口
callable是实现线程的一种处理方式它的优点就是具有返回值还可以有异常的抛出并且在源码中可以看出在futuretask中对源码进行的处理。
理一遍callable的处理流程 1创建callable的实例化对象2把callable的实例化对象作为参数传入FutureTask对象中3把FutureTask作为参数传入创建线程Thread对象中4启动线程start方法
package com.knife.callableDemo;import java.util.concurrent.Callable;public class MyCallable implements CallableString {Overridepublic String call() throws Exception {for (int i1;i10;i){System.out.println(Thread.currentThread().getName()----------i i);}return 成功;}}测试类
package com.knife.callableDemo;import java.util.concurrent.FutureTask;public class DemoCallable {public static void main(String[] args) throws Exception {
// 准备参数MyCallable myCallable new MyCallable();// 准备一个futureTask对象FutureTaskString futureTask new FutureTask(myCallable);
// 创建线程Thread thread new Thread(futureTask);
// 启动线程thread.start();// 等待线程完成后才能够获取返回的结果get()方法获取方法的返回值String s futureTask.get();System.out.println(s);System.out.println(---------------------------------);MyCallable myCallable1 new MyCallable();MyCallable myCallable2 new MyCallable();FutureTaskString futureTask1 new FutureTask(myCallable1);FutureTaskString futureTask2 new FutureTask(myCallable2);Thread thread1 new Thread(futureTask1);Thread thread2 new Thread(futureTask2);// 设置线程的优先级thread1.setPriority(1);
// thread2.setPriority(2);// 获取当前线程的优先级System.out.println(thread1.getPriority());System.out.println(thread2.getPriority());// 启动线程thread1.start();thread2.start();}
}
线程的优先级
每个线程都有一个优先级优先级可以用整数表示取值范围为0~100为最低优先级10位最高优先级当决定哪个线程需要调度时首先查看是否存在优先级高的可调度线程如果存在就从中选择进行调度。Thread类有三个优先级静态常量MAX_PRIORITY为10为线程最高优先级MIN_PRIORITY取值为1为线程最低优先级NORM_PRIORITY取值为5为线程中间位置的优先级。默认情况下线程的优先级为NORM_PRIORITY。特殊情况在于现在计算机都是多核多线程的配置有可能优先级低的线程比优先级高的线程先运行优先级高的线程可能比优先级低的线程后运行。越高的线程获取CPU时间片的次数越多线程start后就纳入到了线程调度器中统一管理线程无权主动索取时间片只能被动分配因此可以通过线程的优先级来最大程度的改善获取时间片 的几率。
多线程
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;}}}
}建议使用实现Runnable接口 多线程售票系统找出问题 问题可能会出现票数超卖例子当票数剩余1的时候三个线程同时访问最后导致票数超卖
//使用多线程模拟三个窗口同时售票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;//让多个线程共享 ticketNumOverridepublic 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;//让多个线程共享 ticketNumOverridepublic 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}}
}
线程终止 public class ThreadExit_ {public static void main(String[] args) throws InterruptedException {T t1 new T();t1.start();//如果希望main线程去控制t1 线程的终止, 必须可以修改 loop//让t1 退出run方法从而终止 t1线程 - 通知方式//让主线程休眠 10 秒再通知 t1线程退出System.out.println(main线程休眠10s...);Thread.sleep(10 * 1000);t1.setLoop(false);}
}class T extends Thread {private int count 0;//设置一个控制变量private boolean loop true;Overridepublic void run() {while (loop) {try {Thread.sleep(50);// 让当前线程休眠50ms} catch (InterruptedException e) {e.printStackTrace();}System.out.println(T 运行中.... (count));}}public void setLoop(boolean loop) {this.loop loop;}
}线程常用方法 public class ThreadMethod01 {public static void main(String[] args) throws InterruptedException {//测试相关的方法T t new T();t.setName(老韩);t.setPriority(Thread.MIN_PRIORITY);//1t.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());//1t.interrupt();//当执行到这里就会中断 t线程的休眠.}
}class T extends Thread { //自定义的线程类Overridepublic void run() {while (true) {for (int i 0; i 100; i) {//Thread.currentThread().getName() 获取当前线程的名称System.out.println(Thread.currentThread().getName() 吃包子~~~~ i);}try {System.out.println(Thread.currentThread().getName() 休眠中~~~);Thread.sleep(20000);//20秒} catch (InterruptedException e) {//当该线程执行到一个interrupt 方法时就会catch 一个 异常, 可以加入自己的业务代码//InterruptedException 是捕获到一个中断异常.System.out.println(Thread.currentThread().getName() 被 interrupt了);}}}
}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(主线程(小弟) 让 子线程(老大) 先吃);//join, 线程插队//t2.join();// 这里相当于让t2 线程先执行完毕Thread.yield();//礼让不一定成功..System.out.println(线程(老大) 吃完了 主线程(小弟) 接着吃..);}}}
}class T2 extends Thread {Overridepublic void run() {for (int i 1; i 20; i) {try {Thread.sleep(1000);//休眠1秒} catch (InterruptedException e) {e.printStackTrace();}System.out.println(子线程(老大) 吃了 i 包子);}}
}public class ThreadMethodExercise {public static void main(String[] args) throws InterruptedException {Thread t3 new Thread(new T3());//创建子线程for (int i 1; i 10; i) {System.out.println(hi i);if(i 5) {//说明主线程输出了5次 hit3.start();//启动子线程 输出 hello...t3.join();//立即将t3子线程插入到main线程让t3先执行}Thread.sleep(1000);//输出一次 hi, 让main线程也休眠1s}}
}class T3 implements Runnable {private int count 0;Overridepublic void run() {while (true) {System.out.println(hello (count));try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}if (count 10) {break;}}}
}用户线程和守护线程 public class ThreadMethod03 {public static void main(String[] args) throws InterruptedException {MyDaemonThread myDaemonThread new MyDaemonThread();//如果我们希望当main线程结束后子线程自动结束//,只需将子线程设为守护线程即可myDaemonThread.setDaemon(true);myDaemonThread.start();for( int i 1; i 10; i) {//main线程System.out.println(宝强在辛苦的工作...);Thread.sleep(1000);}}
}class MyDaemonThread extends Thread {public void run() {for (; ; ) {//无限循环try {Thread.sleep(1000);//休眠1000毫秒} catch (InterruptedException e) {e.printStackTrace();}System.out.println(马蓉和宋喆快乐聊天哈哈哈~~~);}}
}线程的生命周期 public class ThreadState_ {public static void main(String[] args) throws InterruptedException {T t new T();System.out.println(t.getName() 状态 t.getState());t.start();while (Thread.State.TERMINATED ! t.getState()) {System.out.println(t.getName() 状态 t.getState());Thread.sleep(500);}System.out.println(t.getName() 状态 t.getState());}
}class T extends Thread {Overridepublic 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;}}
}Synchronized 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个线程-窗口//测试一把SellTicket03 sellTicket03 new SellTicket03();new Thread(sellTicket03).start();//第1个线程-窗口new Thread(sellTicket03).start();//第2个线程-窗口new Thread(sellTicket03).start();//第3个线程-窗口}
}//实现接口方式, 使用synchronized实现线程同步
class SellTicket03 implements Runnable {private int ticketNum 100;//让多个线程共享 ticketNumprivate boolean loop true;//控制run方法变量Object object new Object();//同步方法静态的的锁为当前类本身//解读//1. public synchronized static void m1() {} 锁是加在 SellTicket03.class//2. 如果在静态方法中实现一个同步代码块./*synchronized (SellTicket03.class) {System.out.println(m2);}*/public synchronized static void m1() {}public static void m2() {synchronized (SellTicket03.class) {//静态方法中锁要加载类本身System.out.println(m2);}}//说明//1. public synchronized void sell() {} 就是一个同步方法//2. 这时锁在 this对象//3. 也可以在代码块上写 synchronize ,同步代码块, 互斥锁还是在this对象public /*synchronized*/ void sell() { //同步方法, 在同一时刻 只能有一个线程来执行sell方法synchronized (/*this*/ object) {//同步代码块//调用同一个对象objectif (ticketNum 0) {System.out.println(售票结束...);loop false;return;}//休眠50毫秒, 模拟try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(窗口 Thread.currentThread().getName() 售出一张票 剩余票数 (--ticketNum));//1 - 0 - -1 - -2}}Overridepublic void run() {while (loop) {sell();//sell方法是一个同步方法}}
}//使用Thread方式
// new SellTicket01().start()
// new SellTicket01().start();
class SellTicket01 extends Thread {private static int ticketNum 100;//让多个线程共享 ticketNum// public void m1() {
// synchronized (this) {
// System.out.println(hello);
// }
// }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;//让多个线程共享 ticketNumOverridepublic 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}}
} 线程死锁 public class DeadLock_ {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();// 保证多线程共享一个对象,这里使用staticstatic Object o2 new Object();boolean flag;public DeadLockDemo(boolean flag) {//构造器this.flag flag;}Overridepublic void run() {//下面业务逻辑的分析//1. 如果flag 为 T, 线程A 就会先得到/持有 o1 对象锁, 然后尝试去获取 o2 对象锁//2. 如果线程A 得不到 o2 对象锁就会Blocked//3. 如果flag 为 F, 线程B 就会先得到/持有 o2 对象锁, 然后尝试去获取 o1 对象锁//4. 如果线程B 得不到 o1 对象锁就会Blocked
//当线程Atrue拿了o1之后需要拿到o2才能继续进行下去而线程Bfalse进入到else语句获取到了o2后也需要获取o1才能继续进行下去之后就导致卡死在这互斥锁里面了if (flag) {synchronized (o1) {//对象互斥锁, 下面就是同步代码System.out.println(Thread.currentThread().getName() 进入1);synchronized (o2) { // 这里获得li对象的监视权System.out.println(Thread.currentThread().getName() 进入2);}}} else {synchronized (o2) {System.out.println(Thread.currentThread().getName() 进入3);synchronized (o1) { // 这里获得li对象的监视权System.out.println(Thread.currentThread().getName() 进入4);}}}}
}
释放锁的操作
释放锁
不会释放锁的操作 线程高级