实现Runnable接口创建线程
package com.cloud.day1; /* 线程的创建方式二: 1.自定义一个类实现Runnable接口 2.实现接口的run方法,把自定义线程的任务定义在run方法中 3.创建Runnable实现类对象 4.创建Thread类实现对象,并把Runnable实现类对象作为参数传递进去 5.调用Thread类对象的start方法开启一个线程 注意: Runnable实现类对象不是线程对象,只有Thread类和其子类创建的对象才是线程对象 */ public class Demo6 implements Runnable{ @Override public void run() { System.out.println(this); System.out.println(Thread.currentThread().getName()); for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+":"+i); } } public static void main(String[] args) { Demo6 d = new Demo6(); Thread th = new Thread(d, "Spring"); th.start(); for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+":"+i); } } } |
购票系统模拟方式2
package com.cloud.day1; /* 创建线程的第二种方式模拟购票系统 */ class SaleTicket2 implements Runnable{ //这里是一种资源给3个线程使用,所以可以不使用static工程成员变量 int num = 50; @Override public void run() { while(true){ synchronized ("锁") { if(num>0){ System.out.println(Thread.currentThread().getName()+"卖出"+num+"号票"); num--; } else{ System.out.println("卖完了..."); break; } } } } } public class Demo7{ public static void main(String[] args) { SaleTicket2 st = new SaleTicket2(); Thread th1 = new Thread(st, "窗口一"); Thread th2 = new Thread(st, "窗口二"); Thread th3 = new Thread(st, "窗口三"); th1.start(); th2.start(); th3.start(); } } |
线程之间的通信
/* 线程通讯:一个线程完成了自己的任务时,要通知另外一个线程去完成另外一个任务. 生产者与消费者 wait(): 等待 如果线程执行了wait方法,那么该线程会进入等待的状态,等待状态下的线程必须要被其他线程调用notify方法才能唤醒。 notify():唤醒 唤醒线程池等待线程其中的一个。 notifyAll() : 唤醒线程池所有等待线程。 wait与notify方法要注意的事项: 1. wait方法与notify方法是属于Object对象的。 2. wait方法与notify方法必须要在同步代码块或者是同步函数中才能使用。 3. wait方法与notify方法必需要由锁对象调用。 问题一:出现了线程安全问题。价格错乱了... */ //产品类 class Product{ String name; //名字 double price; //价格 boolean flag = false; //产品是否生产完毕的标识,默认情况是没有生产完成。 } //生产者 class Producer extends Thread{ Product p ; //产品 public Producer(Product p) { this.p = p ; } @Override public void run() { int i = 0 ; while(true){ synchronized (p) { if(p.flag==false){ if(i%2==0){ p.name = "苹果"; p.price = 6.5; }else{ p.name="香蕉"; p.price = 2.0; } System.out.println("生产者生产出了:"+ p.name+" 价格是:"+ p.price); p.flag = true; i++; p.notifyAll(); //唤醒消费者去消费 }else{ //已经生产完毕,等待消费者先去消费 try { p.wait(); //生产者等待 } catch (InterruptedException e) { e.printStackTrace(); } } } } } } //消费者 class Customer extends Thread{ Product p; public Customer(Product p) { this.p = p; } @Override public void run() { while(true){ synchronized (p) { if(p.flag==true){ //产品已经生产完毕 System.out.println("消费者消费了"+p.name+" 价格:"+ p.price); p.flag = false; p.notifyAll(); // 唤醒生产者去生产 }else{ //产品还没有生产,应该等待生产者先生产。 try { p.wait(); //消费者也等待了... } catch (InterruptedException e) { e.printStackTrace(); } } } } } } public class Demo1 { public static void main(String[] args) { Product p = new Product(); //产品 //创建生产对象 Producer producer = new Producer(p); //创建消费者 Customer customer = new Customer(p); //调用start方法开启线程 producer.start(); customer.start(); } } |
线程的停止
package com.cloud.day1; /* 线程的停止: 1.停止一个线程,一般使用一个变量来控制 2.如果需要停止一个处于等待状态下的线程,需要变量配合notify()和interrupt()使用 */ public class Demo2 extends Thread{ boolean flag=true; public Demo2(String name){ super(name); } @Override public synchronized void run() { int i=0; while(flag){ try { this.wait();//线程进入临时阻塞状态 } catch (InterruptedException e) { System.out.println("接收到了异常"); } System.out.println(Thread.currentThread().getName()+":"+i); i++; } } public static void main(String[] args) { Demo2 de=new Demo2("Spring1"); de.setPriority(10); de.start(); for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+":"+i); //当i=80的时候停止Spring1线程 if(i==80){ de.flag=false; de.interrupt();//强制清除线程的等待阻塞状态,会抛出InterruptedException异常 } } } } |
线程守护
/* 守护线程(后台线程):如果一个进程只剩下守护线程,守护线程也会死亡 需求:模拟软件下载 线程默认都不是守护线程 */ public class Demo1 extends Thread{ public Demo1(String name){ super(); } @Override public void run() { for(int i=0;i<=100;i++){ System.out.println("软件下载了"+i+"%"); if(i==100){ System.out.println("软件下载完毕"); } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { Demo1 demo1=new Demo1("下载程序"); //设置demo1是守护线程 demo1.setDaemon(true); //判断demo1是不是守护线程 System.out.println("守护线程:"+demo1.isDaemon()); demo1.start(); } } |
线程的join()方法
/* join()方法 模拟:两个坐车没买票,其中一个去买票 */ class One extends Thread{ @Override public void run() { System.out.println("坐车没票"); //等待票 System.out.println(".............."); Other other=new Other(); other.start(); try { //join加入:加入新的线程,会让新加入线程先完成任务才会继续执行自己的任务 other.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(".............."); System.out.println("买票上车"); } } class Other extends Thread{ @Override public void run() { System.out.println("去买票"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("有票了"); } } public class Demo2 { public static void main(String[] args) { One one=new One(); one.start(); } } |