美文网首页
Thread线程学习

Thread线程学习

作者: 一条小团团OvO | 来源:发表于2019-07-28 17:58 被阅读0次

一.实现线程的两种方式

1.继承Thread,重写run()方法
创建

package Test;
/**
 * 线程学习代码
 */
public class ThreadStudy extends Thread{
    private String na;
    public ThreadStudy(){

    }
    public ThreadStudy(String na){
        this.na = na;
    }
    public String getNa() {
        return na;
    }

    public void setNa(String na) {
        this.na = na;
    }

    public void run(){
        for (int i = 0; i <100 ; i++) {
            System.out.println(this.getNa()+"跑了"+i+"多少米");
        }
    }
}

调用

package Test;
public class Test {
    public static void main(String[] args) {
      ThreadStudy ts1=new ThreadStudy("加特林");
        ThreadStudy ts3=new ThreadStudy("舒伯特");
        ThreadStudy ts2=new ThreadStudy("留下");
        ts1.start();
        ts2.start();
        ts3.start();
    }
}

2.实现runnable()接口
创建

package Test;
/**
 * 线程学习代码
 */
public class ThreadStudy implements Runnable{
    private String na;
    public ThreadStudy(){

    }
    public ThreadStudy(String na){
        this.na = na;
    }
    public String getNa() {
        return na;
    }

    public void setNa(String na) {
        this.na = na;
    }

    public void run(){
        for (int i = 0; i <100 ; i++) {
            System.out.println(this.getNa()+"跑了"+i+"多少米");
        }
    }
}

调用

package Test;
public class Test {
    public static void main(String[] args) {
      ThreadStudy ts1=new ThreadStudy("加特林");
        ThreadStudy ts3=new ThreadStudy("舒伯特");
        ThreadStudy ts2=new ThreadStudy("留下");
        Thread t1=new Thread(ts1);
        Thread t2=new Thread(ts2);
        Thread t3=new Thread(ts3);
        t1.start();
        t2.start();
        t3.start();
    }
}

3.线程的几种状态,及如何切换
创建对象----就绪状态----执行状态----等待/挂起----异常/消亡
new------start-------run ----- wait--------exception

二.简单的练习(模拟一个12306)

1.创建一个包 12306.PNG

2.代码

package simulation12306;

import java.util.Vector;

public class System12306 {
   private Vector<Ticket> tickets=new Vector<>();
   //设计一个单例(恶汉模式)
   private System12306(){}
   private static System12306 system12306=new System12306();
   public static System12306 getinstance(){
       //懒汉模式在里面new
       return system12306;
   }
   //给tickets赋值
    {
        for (int i = 10; i <100 ; i++) {
           tickets.add(new Ticket("北京"+i,"深圳",(i%5+5)*25f));
        }
    }
   public Ticket getTicket(){
        if(tickets.isEmpty()){
            return null;
        }else {
            return tickets.remove(0);
        }
   }
}

package simulation12306;

public class Testmain {
    public static void main(String[] args) {
        Window w1=new Window("窗口1");
        Window w2=new Window("窗口2");
        Window w3=new Window("窗口3");
        Window w4=new Window("窗口4");
        w1.start();
        w2.start();
        w3.start();
        w4.start();
    }
}

package simulation12306;

public class Window extends Thread{
    private String windowName;
    public Window(){

    }
    public Window(String windowName){
      this.windowName=windowName;
    }
    public void run(){
        sellTicket();
    }
    public void sellTicket(){
        System12306 ss=System12306.getinstance();
        while (true){
            Ticket ticket=ss.getTicket();
            if(ticket==null){
                System.out.println(windowName+"窗口车票已卖完");
                break;
            }
            System.out.println(this.windowName+"卖出"+ticket.getStart()+"的车票");
        }
    }
}

package simulation12306;

public class Ticket {
    private String start;//起始站
    private String end;//终点站
    private Float price;//价格
    public Ticket(String start,String end,Float price){
        this.start = start;
        this.end = end;
        this.price = price;
    }
    public Ticket(){

    }
    public String getStart() {
        return start;
    }

    public void setStart(String start) {
        this.start = start;
    }

    public String getEnd() {
        return end;
    }

    public void setEnd(String end) {
        this.end = end;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }
}

三.生产消费模型.

1.代码

package producerconsumbermodel;

import java.util.ArrayList;
import java.util.List;

public class Warehouse{
    private List<String> list=new ArrayList<>();//线程非安全
    public void setList(){
        if(list.size()<20){
            list.add("aaa");
            System.out.println("生产了一个商品");
        }else{
            return;
        }
    }
    public void getList(){
        if(list.size()>0){
            list.remove(0);
            System.out.println("拿走了一个商品");
        }else{
            return;
        }
    }
}

package producerconsumbermodel;

public class Producer extends Thread{
    private Warehouse warehouse;
    public Producer(){

    }
    public Producer(Warehouse warehouse) {
        this.warehouse = warehouse;
    }

    public void run(){
        while (true){
            warehouse.setList();
            try {
                Thread.sleep(200);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
}

package producerconsumbermodel;

public class Consumber extends Thread{
    private Warehouse warehouse;
    public Consumber(Warehouse warehouse) {
        this.warehouse = warehouse;
    }

    public void run(){
        while (true){
            warehouse.getList();
            try {
                Thread.sleep(300);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

package producerconsumbermodel;

public class test {
    public static void main(String[] args) {
        Warehouse w=new Warehouse();
        Producer producer=new Producer(w);
        producer.start();
        Consumber c1=new Consumber(w);
        Consumber c2=new Consumber(w);
        Consumber c3=new Consumber(w);
        c1.start();
        c2.start();
        c3.start();

    }
}

2.线程安全问题


报错.PNG

2.1出现的原因


线程安全.PNG

会出现两个异常,一.如上,二.IllegalMonitorStateException非法监听异常
3.解决线程安全的问题(特征修饰符synchronized同步,线程锁)
两种写法:
1.放在方法的结构上public synchronized void test(){}锁定的是调用方法时的那个对象

package producerconsumbermodel;

import java.util.ArrayList;
import java.util.List;

public class Warehouse{
    private List<String> list=new ArrayList<>();//线程非安全
    public synchronized void setList(){
        if(list.size()<20){
            list.add("aaa");
            System.out.println("生产了一个商品");
        }else{
            System.out.println("生产满了,等一会再生产");
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public synchronized void getList(){
        if(list.size()>0){
            list.remove(0);
            System.out.println("拿走了一个商品");
        }else{
            System.out.println("等你生产");
            try {
                this.notifyAll();//唤醒所有线程
                this.wait();//this指的是调用当前对象的线程等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

2.放在方法内部public void test(){代码synchronized(){代码}代码}锁定的是调用方法时的那个对象
小结.
wait和sleep的区别


区别.PNG

四.线程死锁

https://www.cnblogs.com/xiaoxi/p/8311034.html

相关文章

  • android 多线程 — Thread

    java 的线程类型是 Thread ,所以多线程的学习也是从 Thread 开始的 继承 Thread 类 启动...

  • Thread线程学习

    一.实现线程的两种方式 1.继承Thread,重写run()方法创建 调用 2.实现runnable()接口创建 ...

  • Java 线程:创建、属性以及状态控制

    前言 本文将对 Java 线程 Thread 进行学习和总结,以下是概览: 一、Thread 创建 线程的创建主要...

  • 线程协程WWW

    线程:Thread // 新线程-1 调用静态方法 Thread m_Thread_1 = new Thread(...

  • Thread

    线程机制理解开启线程: Thread thread = new Thread(Runnable).start();...

  • 守护线程(Daemon)

    线程分类 User Thread(用户线程) Daemon Thread(守护线程) 定义: 守护线程--也称“服...

  • 多线程

    Java多线程----Thread java使用Thread类代表线程,所有的线程对象必须是Thread类或者其子...

  • 守护线程

    在Java中有两类线程:用户线程 (User Thread)、守护线程 (Daemon Thread)。 守护线程...

  • 守护线程(Daemon Thread)

    守护线程(Daemon Thread) 在Java中有两类线程:用户线程 (User Thread)、守护线程 (...

  • 4-Thread类详解与线程的状态分析

    Thread类详解与线程的状态分析 线程的状态 在正式学习Thread类中的具体方法之前,我们先来了解一下线程有哪...

网友评论

      本文标题:Thread线程学习

      本文链接:https://www.haomeiwen.com/subject/skyulctx.html