Java多线程

本文介绍了Java中创建线程的两种方式:继承Thread类和实现Runnable接口,并通过示例展示了如何使用这两种方式。此外,还详细讲解了线程同步的重要性及其实现方法,包括同步块和同步方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Java中主要提供两种方式实现线程,分别为继承java.lang.Thread类与实现java.lang.Runnable接口。

public class ThreadTest extends Thread {
    private int count = 10;

    @Override
    public void run() { // 重写run()方法
        while (true) {
            System.out.print(count + " "); // 打印count变量
            if (--count == 0) { // 使count变量自减,当自减为0时,退出循环
                return;
            }
        }
    }

    public static void main(String[] args) {
        new ThreadTest().start();
    }
}

如果start()方法调用一个已经启动的线程,系统将抛出IllegalThreadStateException异常。

public class ThreadTest extends Thread {
    private int count = 10;

    @Override
    public void run() { // 重写run()方法
        while (true) {
            System.out.print(count + " "); // 打印count变量
            if (--count == 0) {
                return;
            }
        }
    }

    public static void main(String[] args) {
        ThreadTest t = new ThreadTest();
        t.start();
        int a = Thread.MIN_PRIORITY;
        try {
            t.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


线程安全

public class ThreadSafeTest implements Runnable {
    int num = 10;

    @Override
    public void run() {
        while (true) {
            if (num > 0) {
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("tickets" + num--);
            }
        }
    }

    public static void main(String[] args) {
        ThreadSafeTest t = new ThreadSafeTest(); // 实例化类对象
        Thread tA = new Thread(t); // 以该类对象分别实例化4个线程
        Thread tB = new Thread(t);
        Thread tC = new Thread(t);
        Thread tD = new Thread(t);
        tA.start(); // 分别启动线程
        tB.start();
        tC.start();
        tD.start();
    }
}


线程同步机制

1.同步块

public class ThreadSynchronizedTest implements Runnable {
    int num = 10;

    @Override
    public void run() {
        while (true) {
            synchronized ("") {
                if (num > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("tickets" + num--);
                }
            }
        }
    }

    public static void main(String[] args) {
        ThreadSynchronizedTest t = new ThreadSynchronizedTest(); // 实例化类对象
        Thread tA = new Thread(t); // 以该类对象分别实例化4个线程
        Thread tB = new Thread(t);
        Thread tC = new Thread(t);
        Thread tD = new Thread(t);
        tA.start(); // 分别启动线程
        tB.start();
        tC.start();
        tD.start();
    }
}


2.同步方法

public class ThreadSynchronizedTest implements Runnable {
    int num = 10;

    public synchronized void doSynchronization() { // 定义同步方法
        if (num > 0) {
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("tickets" + num--);
        }
    }

    @Override
    public void run() {
        while (true) {
            doSynchronization();
        }
    }

    public static void main(String[] args) {
        ThreadSynchronizedTest t = new ThreadSynchronizedTest(); // 实例化类对象
        Thread tA = new Thread(t); // 以该类对象分别实例化4个线程
        Thread tB = new Thread(t);
        Thread tC = new Thread(t);
        Thread tD = new Thread(t);
        tA.start(); // 分别启动线程
        tB.start();
        tC.start();
        tD.start();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值