Java 多线程

2种线程的实现

继承Thread类

  • 必须重写run()方法
示例
  • Mythread.java

public class Mythread extends Thread {
    private String name;

    public Mythread(String name) {
        this.name = name;//通过构造方法创建线程的名字
    }

    //重写run 方法
    public  void run(){
        for (int i = 0; i < 10; i++) {
            System.out.println(this.name +":"+i);
        }
        super.run();
    } 
}
  • ThreadTest.java

public class ThreadTest {

    public static void main(String[] args) {
        // 这里创建两个线程并开启线程,查看并发
         Mythread a = new Mythread("a");
         Mythread b = new Mythread("b");
         a.start();//线程的启用
         b.start();
    }
}

实现Runnable接口

  • 必须重写run()方法
示例
  • Myrunable.java

public class Myrunable implements Runnable {

    private String name;

    public Myrunable (String name) {
        this.name = name;//通过构造方法创建线程的名字
    }

    //重写run 方法
    public  void run(){
        for (int i = 0; i < 10; i++) {
            System.out.println(this.name +":"+i);
        }
    } 

}
  • Threadtest.java

public class ThreadTest {

    public static void main(String[] args) {
        Myrunable r1 = new Myrunable("a");
        Myrunable r2 = new Myrunable("b");
        // 给每个runable对象创建线程
        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        // 通过start方法启用线程
        t1.start();
        t2.start();
    }
}

线程常用方法

getName();//获取线程名称
currentThread();//获取当前线程
isAlive();//判断线程是否存活
join();//线程强行运行
yield();//线程礼让,和jion对立
sleep();//线程睡眠

线程的优先级

这里写图片描述

示例


class test implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } // 睡眠1s

        for (int j = 0; j < 1000; j++) {

            System.out.println(Thread.currentThread().getName() + ":" + j);
        }
    }

}
public class ThreadTest {

    public static void main(String[] args) {
    //通过匿名对象创建
        Thread t1 = new Thread(new test() , "a");
        Thread t2 = new Thread(new test() , "b");
        Thread t3 = new Thread(new test() , "c");
      //设置线程优先级
        t1.setPriority(Thread.MIN_PRIORITY);
        t2.setPriority(Thread.MAX_PRIORITY);
        t3.setPriority(Thread.NORM_PRIORITY);
        //启动线程
        t1.start();
        t2.start();
        t3.start();

    }
}

同步

  • 购票示例
//资源同步
class test implements Runnable {

    private int ticket = 50;

    public void run() {
        for (int i = 0; i < 100; i++) {
            synchronized (this) {
                if (ticket > 0) {
                    System.out.println(Thread.currentThread().getName() + "车票" + ticket--);
                }
            }
        }
    }
}
public class ThreadTest {

    public static void main(String[] args) {
        test m1 = new test();
        Thread t1 = new Thread(m1);
        Thread t2 = new Thread(m1);
        Thread t3 = new Thread(m1);
        t1.start();
        t2.start();
        t3.start();
    }
}
//方法同步

class test implements Runnable {

    private int ticket = 50;

    public void run() {
        for (int i = 0; i < 100; i++) {
            tell();
        }
    }

    public synchronized void tell() {

        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + "车票" + ticket--);
        }

    }
}

public class ThreadTest {

    public static void main(String[] args) {
        test m1 = new test();
        Thread t1 = new Thread(m1);
        Thread t2 = new Thread(m1);
        Thread t3 = new Thread(m1);
        t1.start();
        t2.start();
        t3.start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值