JavaSE-day16

多线程

多线程分为两种实现方式:

1.继承Thread类

2.实现Runnable接口

一,继承Thread类

继承类需重写run方法,想用多线程的方式启动时,用start()方法进行启动

/*本类用于实现多线程编程方案1:继承Thread类的方式*/
public class TestThread {
    public static void main(String[] args) {
        //4.创建自定义线程类对象
        MyThread th = new MyThread();/*对应的是线程的新建状态*/
        MyThread th1 = new MyThread();
        MyThread th2 = new MyThread();
        MyThread th3 = new MyThread();

//        //5.1测试1:自己主动调用run(),并没有多线程的效果
//        th.run();
//        th1.run();
//        th2.run();
//        th3.run();

        //5.1测试2:调用start(),以多线程的效果运行
        th.start();/*对应的是线程的就绪状态*/
        th1.start();
        th2.start();
        th3.start();
    }
}
//1.自定义线程类,并且让这个类继承Thread类
class MyThread extends Thread{
    //2.重写父类的run()方法
    @Override
    public void run() {
        //3.在run()完成自己的业务
        //打印十次当前正在干活的线程名
        for (int i = 0; i <10 ; i++) {
            System.out.println(getName());
        }
    }
}

二,实现Runnable接口

实现该接口想用获取正在执行的对象名,需先在Thread类中寻找currentThread(),获取正在运行的对象,然后再使用getName()获取对象名

/*本类用于多线程编程实现方案二:实现Runnable接口*/
public class TestRunnable {
    public static void main(String[] args) {
        //4.创建自定义线程类对象--作为业务对象
        MyRunnable t = new MyRunnable();
        //5.创建线程对象,并将业务对象交给线程对象
        Thread thread = new Thread(t);
        Thread thread1 = new Thread(t);
        Thread thread2 = new Thread(t);
        Thread thread3 = new Thread(t);
        //6.以多线程的方式启动线程
        thread.start();
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

//1.定义多线程类
class MyRunnable implements Runnable{

    //2.添加父接口中未实现的抽象方法
    @Override
    public void run() {
        //3.完成业务
        for (int i = 0; i < 10; i++) {
            /*问题:自定义类与父接口Runnable中都没有获取名字的方法
            * 所有还需要从Thread类里找
            * currentThread():静态方法,获取当前正在执行线程的对象
            * getName():获取当前正在执行的线程对象的名称*/
            System.out.println(Thread.currentThread().getName()+"="+i);
        }
    }
}

三,分别使用两种方式实现买票案例

Thread:
public class TestThread {
    public static void main(String[] args) {
        MyThread my1 = new MyThread();
        MyThread my2 = new MyThread();
        MyThread my3 = new MyThread();
        MyThread my4 = new MyThread();

        my1.start();
        my2.start();
        my3.start();
        my4.start();
    }
}
class MyThread extends Thread{
    static int tickets=100;
    @Override
    public void run() {
        while (true){
            synchronized (MyThread.class){
                if (tickets<=0) break;
                System.out.println(getName()+"--"+tickets--);
            }
        }
    }
}
Runnable:(还使用了线程池)
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestRunnable {
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
//        Thread th1 = new Thread(myRunnable);
//        Thread th2 = new Thread(myRunnable);
//        Thread th3 = new Thread(myRunnable);
//        Thread th4 = new Thread(myRunnable);
//
//        th1.start();
//        th2.start();
//        th3.start();
//        th4.start();
        ExecutorService ex = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            ex.execute(myRunnable);
        }
    }
}

class MyRunnable implements Runnable{
    int tickets=100;

    @Override
    public void run() {
        while (true){
            synchronized (new MyRunnable().getClass()){
                if (tickets<=0) break;
                System.out.println(Thread.currentThread().getName()+"--"+tickets--);
            }
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值