Java多线程

目录

一、程序、进程与线程概念

二、多线程

1.多线程概念

2.实现多线程的方式

    a.生成Thread类的子类,并定义该子类自的run()方法,线程要完成的任务在run()方法中实现。

    b.实现Runnable接口,通过实现Runnable接口中的un()方法来完成线程的任务。

三、线程中的Sleep方法

四、通过线程实现卖票


一、程序、进程与线程概念

1.程序:程序是一组指令的有序集合,它本身没有任何运行的含义,它只是一个静态的实体。

2.进程:进程是程序在某个数据集上的执行。进程是一个动态的实体,它有自己的生命周期。它因创建而产生,因调度而运行,因等待资源或事件而被处于等待状态,因完成任务而被撤消,反映了一个程序在一定的数据集上运行的全部动态过程。

3.线程:线程是进程内一个相对独立的、可调度的执行单元,又称为轻量级进程。和进程类似,线程也有运行、就绪、阻塞等状态。线程必须拥有父进程。系统没有为线程分配资源,它与进程中的其他线程共享该进程的共享资源。

二、多线程

1.多线程概念

        多线程程序是指一个程序中包含多个执行流,它是实现并发机制的有效手段。从逻辑的观点看,多线程意味着一个程序的多个语句块同时执行,但不等于多次启动一个程序。

2.实现多线程的方式

    a.生成Thread类的子类,并定义该子类自的run()方法,线程要完成的任务在run()方法中实现。

public class MyThread extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("MyThread------" + i);
        }
    }
}
package study2;
public class Test {
    public static void main(String[] args) {
        MyThread t=new MyThread();
        t.start();
        for (int i = 0; i < 5; i++) {
            System.out.println("main------"+i);
        }
    }
}

    b.实现Runnable接口,通过实现Runnable接口中的un()方法来完成线程的任务。

public class MyThread implements Runnable {
    String threadId;
    public MyThread(String threadId){
        this.threadId=threadId;
    }
    public void run() {
        System.out.println(this.threadId+"开始工作");
        for (int i = 0; i < 6; i++) {
            System.out.println(this.threadId+": i="+(i+1)+"\t");
        }
        System.out.println(this.threadId+"结束工作");
    }
}
public class Test {
    public static void main(String[] args) {
        Runnable r1 = new MyThread("线程1");
        Thread t1=new Thread(r1);
        t1.start();
        Runnable r2 = new MyThread("线程2");
        Thread t2=new Thread(r2);
        t2.start();
        Runnable r3 = new MyThread("线程3");
        Thread t3=new Thread(r3);
        t3.start();
    }
}

三、线程中的Sleep方法

        让当前线程暂时停止下来,睡眠的时间由指定的毫秒数决定。线程醒来之后返回到就绪状态。sleep()方法指定的时间是线程不会运行的最短时间,而不是不会运行的实际时间。

static void sleep(long millis)
throws IllegalArgumentException,InterruptedException
参数millis:以毫秒为单位的睡眠时间长度。
异常IllegalArgumentException:如果millis值为负数。
异常InterruptedException:如果任何线程中断当前线程。当抛出此异常时,当前线程的中断状态将被清除。
public class MyThread implements Runnable {
    String threadId;

    public MyThread(String threadId) {
        this.threadId = threadId;
    }

    public void run() {
        System.out.println(this.threadId + "开始工作");
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(this.threadId + "正在沉睡!");
        }
        for (int i = 0; i < 6; i++) {
            System.out.println(this.threadId + ": i=" + (i + 1) + "\t");
        }
        System.out.println(this.threadId + "结束工作");
    }
}
public class Test {
    public static void main(String[] args) {
        Runnable r1 = new MyThread("线程1");
        Thread t1=new Thread(r1);
        t1.start();

        Runnable r2 = new MyThread("线程2");
        Thread t2=new Thread(r2);
        t2.start();
    }
}

四、通过线程实现卖票

public class Tickets {
    int num;

    public Tickets(int num) {
        this.num=num;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num=num;
    }
}
public class Operate {
    private Tickets tickets;

    public Operate(Tickets tickets) {
        this.tickets = tickets;
    }

    public void sale() {
        synchronized (this) {
            if (tickets.getNum() > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (tickets.getNum() > 0) {
                    int num = tickets.getNum();
                    System.out.println(Thread.currentThread().getName() + "正在卖第" + tickets.getNum()+ "张票,剩余" + --num);
                    tickets.setNum(num);
                }
            }
        }
    }
}
package study1;

public class Test {
    public static void main(String[] args) {
        Tickets tickets = new Tickets(100);
        Operate operate = new Operate(tickets);
        new Thread(() -> {
            while (tickets.getNum() > 0) {
                operate.sale();
            }
        }, "窗口1").start();

        new Thread(() -> {
            while (tickets.getNum() > 0) {
                operate.sale();
            }
        }, "窗口2").start();

        new Thread(() -> {
            while (tickets.getNum() > 0) {
                operate.sale();
            }
        }, "窗口3").start();

        new Thread(() -> {
            while (tickets.getNum() > 0) {
                operate.sale();
            }
        }, "窗口4").start();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值