赞
踩
简单点说:程序就是我们写的代码;也可以理解为:为完成特定任务,用某种语言编写的一组指令的集合
public class Thread01 { public static void main(String[] args) { Cat cat = new Cat(); cat.start();//启动线程-> main 线程启动一个子线程 Thread-0 最终会执行 cat 的 run 方法 //为什么调用start方法 // (1) // public synchronized void start() { // start0(); // } // (2) //start0() 是本地方法,是 JVM 调用, 底层是 c/c++实现 //真正实现多线程的效果, 是 start0(), 而不是 run // private native void start0(); // 当 main 线程启动一个子线程 Thread-0, 主线程不会阻塞, 会继续执行 //这时 主线程和子线程是交替执行.. System.out.println("主线程的线程名" + Thread.currentThread().getName()); for(int i = 0; i < 60; i++) { System.out.println("主线程 i=" + i); //让主线程休眠 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Cat extends Thread { int times = 0; @Override public void run() {//重写run方法,写上自己的业务逻辑 //该线程每隔1秒,在控制台输出"喵喵,我是小猫咪" while (true) { System.out.println("喵喵,我是小猫咪" + "线程名=" + (++times) + Thread.currentThread().getName()); //让该线程休眠1秒 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if (times == 80) { break;//当times到80,退出while,这是线程也就退出 } } } }
调用start()方法示意图
注意:主线程和子线程会交替执行(并发/并行)
说明:
1,java是单继承的,在某些情况下一个类可能已经继承了某个父类,这时再用继承Thread类方法来创建线程显然不可能了
2,java设计者们提供了另一个方式创建线程,就是通过实现Runnable接口来创建线程
public class Thread02 { public static void main(String[] args) { Dog dog = new Dog(); // dog.start(); // public Thread(Runnable target) { // this(null, target, "Thread-" + nextThreadNum(), //代理模式: Thread类有个如上构造器,需要传入Runnable类型的参数 // Dog正好实现了Runnable类型 // Dog类型可以代理Runnable类型传入 Thread thread = new Thread(dog); thread.start(); } } class Dog implements Runnable{ int times = 0; @Override public void run() { while (true) { System.out.println("hi" + (++times) + Thread.currentThread().getName()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if (times==10) { break;} } } }
public class Thread03 { public static void main(String[] args) { T1 t1 = new T1(); T2 t2 = new T2(); Thread thread = new Thread(t1); Thread thread1 = new Thread(t2); thread1.start();//启动第 1 个线程 thread.start();//启动第 2 个线程 } } class T1 implements Runnable{ int count = 0 ; @Override public void run() { while (true){ System.out.println("hello,world"+(++count)+Thread.currentThread().getName()); 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) + Thread.currentThread().getName()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if (count == 10) { break; } } } }
出现超卖现象
public class Sell { public static void main(String[] args) { SellTicket sellTicket = new SellTicket(); //这里会出现超卖现象 new Thread(sellTicket).start(); new Thread(sellTicket).start(); new Thread(sellTicket).start(); } } class SellTicket implements Runnable { public int ticketNum = 100; @Override public void run() { while (true) { if (ticketNum <= 0) { System.out.println("售票结束"); break; } try { Thread.sleep(12); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("窗口"+Thread.currentThread().getName()+"售出一张票"+"剩余票数"+(--ticketNum)); } } }
public class ThreadExit { public static void main(String[] args) { AThread aThread = new AThread(); Thread thread = new Thread(aThread); thread.start(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } //4,更改AThread类中的loop变量,停止线程 aThread.setLoop(false); } } class AThread implements Runnable{ //1,定义标记变量,默认为true private boolean loop =true; //2,将loop作为循环条件 @Override public void run() { while (loop){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("AThread运行中"); } } //3,提高公共的set方法,用于更细loop public void setLoop(boolean loop) { this.loop = loop; } }
1,start底层会创建新的线程,调用run,run就是一个简单的方法调用,不会再启动新线程
2,线程优先级的范围
3,interrupt:中断线程。但并没有真正的结束线程,所以一般用于中断正在休眠的线程
4,sleep:线程的静态方法,使当前线程休眠
方法的案例演示
public class ThreadMethod { public static void main(String[] args) { ThreadDemo threadDemo = new ThreadDemo(); threadDemo.setName("小笼包"); //设置优先级 threadDemo.setPriority(Thread.MIN_PRIORITY); threadDemo.start(); for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println("hi"+i); } //测试优先级 System.out.println(threadDemo.getName()+"线程的优先级"+threadDemo.getPriority()); threadDemo.interrupt();//当执行到这里,就会中断线程的休眠 } } class ThreadDemo extends Thread{ @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName()+"吃包子~~~"+i); } try { System.out.println("休眠中~~~~~"); Thread.sleep(20000); } catch (InterruptedException e) { //当该线程执行到Interrupt方法时,会catch一个异常,可以加入自己的业务代码 //InterruptedException 是捕获到一个中断异常 System.out.println(Thread.currentThread().getName()+"被interrupt了"); } } }
public class ThreadMethod02 { public static void main(String[] args) throws InterruptedException { T t = new T(); t.start(); for (int i = 0; i < 5; i++) { System.out.println("主线程(小弟)"+i); Thread.sleep(500); System.out.println("主线程(小弟)让 子线程(老大)先吃"); // 线程插队:先执行完插入的所有任务(t 子线程中),再执行主线程任务 t.join(); //礼让 不一定成功 // Thread.yield(); } } } class T extends Thread{ @Override public void run() { for (int i = 0; i < 20; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println("Hello~~~"+i); } } }
守护线程的案例
public class ThreadMethod03 { public static void main(String[] args) { MyDaemon myDaemon = new MyDaemon(); //为MyDaemon类 添加守护线程:当所有的用户线程结束,守护线程自动结束 myDaemon.setDaemon(true); myDaemon.start(); for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println("主线程" ); } } } class MyDaemon extends Thread{ @Override public void run() { for(;;){ try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println("守护线程"); } } }
public class ThreadState_ { public static void main(String[] args) throws InterruptedException { TT tt = new TT(); System.out.println(tt.getName()+"状态"+tt.getState());//状态:NEW RUNNABLE tt.start(); while (Thread.State.TERMINATED!=tt.getState()){ System.out.println(tt.getName()+"状态"+tt.getState());//状态:TIMED_WAITING Thread.sleep(500); } System.out.println(tt.getName()+"状态"+tt.getState());//状态:TERMINATED } } class TT 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) { throw new RuntimeException(e); } } break; } } }
public class Sell { public static void main(String[] args) { SellTicket sellTicket = new SellTicket(); //这里会出现超卖现象 new Thread(sellTicket).start(); new Thread(sellTicket).start(); new Thread(sellTicket).start(); } } class SellTicket implements Runnable { public int ticketNum = 100; public synchronized void m(){//同步方法,在同一时刻,只能有一个线程来执行sell方法 while (true) { if (ticketNum <= 0) { System.out.println("售票结束"); break; } try { Thread.sleep(12); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("窗口"+Thread.currentThread().getName()+"售出一张票"+"剩余票数"+(--ticketNum)); } } @Override public void run() { m(); } }
public class Sell { public static void main(String[] args) { // SellTicket sellTicket = new SellTicket(); // //这里会出现超卖现象 // new Thread(sellTicket).start(); // new Thread(sellTicket).start(); // new Thread(sellTicket).start(); SellTicket sellTicket2 = new SellTicket(); Thread thread1 = new Thread(sellTicket2); thread1.start(); Thread thread2 = new Thread(sellTicket2); thread2.start(); Thread thread3 = new Thread(sellTicket2); thread3.start(); } } class SellTicket implements Runnable { public int ticketNum = 100; private boolean loop = true; Object object = new Object(); //1. public synchronized static void m1() {} 锁是加在 SellTicket03.class //2. 如果在静态方法中,实现一个同步代码块. /* synchronized (SellTicket.class) { System.out.println("m2"); }*/ //1. public synchronized void sell() {} 就是一个同步方法 //2. 这时锁在 this 对象 //3. 也可以在代码块上写 synchronize ,同步代码块, 互斥锁还是在 this 对象 public /*synchronized*/ void m(){//同步方法,在同一时刻,只能有一个线程来执行sell方法 synchronized (/*this*/object) { while (loop) { if (ticketNum <= 0) { System.out.println("售票结束"); break; } try { Thread.sleep(12); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("窗口" + Thread.currentThread().getName() + "售出一张票" + "剩余票数" + (--ticketNum)); } } } @Override public void run() { m(); } }
多个线程都占用了对方的锁资源,但不肯想让,导致了死锁,在编程中一定要避免死锁发生
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。