16_多线程,16多线程
分享于 点击 8642 次 点评:28
16_多线程,16多线程
多线程 什么是进程? 应用程序的一次运行产生进程。 为什么存在进程的概念? 什么是线程 参考:https://www.cnblogs.com/geeta/p/9474051.html 线程和进程区别![]() |
package cn.sxt01.thread01; public class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("MyThread:" + i); } } } |
package cn.sxt01.thread01; public class Test01 { public static void main(String[] args) { // 【1】创建一个线程并执行 MyThread myThread = new MyThread(); myThread.start(); // main线程也称主线程 for (int i = 0; i < 10; i++) { System.out.println("MainThread:" + i); } } } |
package cn.sxt01.thread01; public class MyRunnable implements Runnable{ @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("MyRunnable:" + i); } } } |
package cn.sxt02.thread02; public class TicketThread extends Thread { private static int count = 5; public TicketThread(String name) { super(name); } @Override public void run() { // 模拟买票 for (int i = 0; i < 5; i++) { if (count > 0) { count--; System.out.println(super.getName() + "卖出一张,还剩" + count + "张"); } } } } |
窗口A卖出一张,还剩4张 窗口B卖出一张,还剩2张 窗口C卖出一张,还剩3张 窗口B卖出一张,还剩0张 窗口A卖出一张,还剩1张 |
for (int i = 0; i < 5; i++) { if (count > 0) { count--; System.out.println(super.getName() + "卖出一张,还剩" + count + "张"); } } |
A抢占到CPU,count(5),条件成立,count--,输出 窗口A卖出一张,还剩4张 C抢占到CPU, |
for (int i = 0; i < 5; i++) { if (count > 0) { count--; System.out.println(super.getName() + "卖出一张,还剩" + count + "张"); } } |
C开始执行,执行到super.getName() + "卖出一张,还剩" + count + "张" 准备好,线程C挂起。 B抢占到CPU |
for (int i = 0; i < 5; i++) { if (count > 0) { count--; System.out.println(super.getName() + "卖出一张,还剩" + count + "张"); } } |
B开始执行,指定到输出位置,输出 窗口B卖出一张,还剩2张 CPU时间片到, |
for (int i = 0; i < 5; i++) { if (count > 0) { count--; System.out.println(super.getName() + "卖出一张,还剩" + count + "张"); } } |
C抢占到CPU,直接冲上次挂起位置开始执行,输出 窗口C卖出一张,还剩3张 |
public class MyRun implements Runnable { private int count = 5; @Override public void run() { // 模拟买票 for (int i = 0; i < 5; i++) { if (count > 0) { count--; System.out.println(Thread.currentThread().getName()+"卖出一张,还剩" + count + "张"); } } } } |
package cn.sxt02.thread02; public class Test02 { public static void main(String[] args) { MyRun run = new MyRun(); Thread t1 = new Thread(run,"窗口A"); Thread t2 = new Thread(run,"窗口B"); Thread t3 = new Thread(run,"窗口C"); t1.start(); t2.start(); t3.start(); } } |
package cn.sxt03.thread03; public class Test02Priority { public static void main(String[] args) { System.out.println(Thread.currentThread().getPriority()); // 线程优先级的最大最小默认值 System.out.println(Thread.MAX_PRIORITY); System.out.println(Thread.MIN_PRIORITY); System.out.println(Thread.NORM_PRIORITY); Thread02 t2 = new Thread02("线程B"); t2.setPriority(Thread.NORM_PRIORITY); Thread02 t1 = new Thread02("线程A"); t1.setPriority(Thread.MAX_PRIORITY); t1.start(); t2.start(); } } |
package cn.sxt03.thread03; public class Test03isAlive { public static void main(String[] args) { Thread t1 = new Thread("线程A"); System.out.println(t1.isAlive()); t1.start(); System.out.println(t1.isAlive()); } } |
public class Test04Join { public static void main(String[] args) { Thread04 t1 = new Thread04("线程A"); t1.start(); for (int i = 0; i < 5; i++) { if(i == 3) { try { t1.join(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("mainThread" + " i=" + i); } } } |
public class Test06Yield { public static void main(String[] args) { Thread06 t1 = new Thread06("线程A"); t1.start(); for (int i = 0; i < 5; i++) { if(i == 3) { Thread.yield(); } System.out.println("mainThread" + " i=" + i); } } } |
可能的结果 mainThread i=0 mainThread i=1 mainThread i=2 => 主线程礼让一次,下次有可能调度主线程,也有可能调度t1 mainThread i=3 mainThread i=4 线程A i=0 线程A i=1 线程A i=2 线程A i=3 线程A i=4 |
synchronized ( 同步监视器/互斥锁 ){ 原子性操作 } |
package cn.sxt04.thread04; public class MyRun implements Runnable { private int count = 5; @Override public void run() { // 模拟买票 for (int i = 0; i < 5; i++) { // mutex 互斥锁 // 通常将当前对象作为同步对象/互斥锁 synchronized (this) { if (count > 0) { count--; try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "卖出一张,还剩" + count + "张"); } } } } } |
package cn.sxt04.thread04; public class MyRun implements Runnable { private int count = 5; @Override public void run() { // 模拟排队买票 for (int i = 0; i < 5; i++) { this.buyTicket(); } } // 同步方法默认把当前对象this加锁 public synchronized void buyTicket() { if (count > 0) { count--; try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "卖出一张,还剩" + count + "张"); } } } |
- synchronized(obj){}中的obj称为同步监视器
- 同步代码块中同步监视器可以是任何对象,但是推荐使用共享资源作为同步监视器
- 同步方法中无需指定同步监视器,因为同步方法的监视器是this,也就是该对象本身
- 第一个线程访问,锁定同步监视器,执行其中代码
- 第二个线程访问,发现同步监视器被锁定,无法访问
- 第一个线程访问完毕,解锁同步监视器
- 第二个线程访问,发现同步监视器未锁,锁定并访问
package cn.sxt05.thread07; public class ThreadA extends Thread{ private Object r1; private Object r2; public ThreadA(Object r1, Object r2) { super(); this.r1 = r1; this.r2 = r2; } @Override public void run() { synchronized (r1) { System.out.println("已拥有r1"); synchronized (r2) { System.out.println("想申请拥有r2"); } } } } |
相关文章
- 暂无相关文章
用户点评