java 3cschool_Java 多线程

本文介绍了Java中的多线程概念,展示了如何创建和启动线程。重点讲解了线程同步的互斥同步和条件同步,以及同步关键字`synchronized`的使用方法,包括同步方法和同步块。此外,还探讨了`wait()`、`notify()`方法在同步控制中的作用,并通过示例代码对比了同步与非同步执行的区别。
摘要由CSDN通过智能技术生成

Java线程教程 - Java多线程

以下代码显示了如何在程序中运行多线程。public class Main {

public static void main(String[] args) {

// Create two Thread objects Thread t1 = new Thread(Main::print);

Thread t2 = new Thread(Main::print);

// Start both threads t1.start();

t2.start();

}

public static void print() {

for (int i = 1; i <= 500; i++) {

System.out.println(i);

}

}

}

上面的代码生成以下结果。

3755b6c43100820cee49d3368f1b4e77.png

线程同步

Java编程语言内置了两种线程同步:互斥同步

条件同步

在互斥同步中,在一个时间点只允许一个线程访问代码段。

条件同步通过条件变量和三个操作来实现:等待,信号和广播。

同步关键字

synchronized关键字用于声明需要同步的关键部分。

有两种方法可以使用synchronized关键字:将方法声明为关键部分

将语句块声明为关键段

我们可以通过在方法的返回类型之前使用关键字synchronized来声明一个方法作为临界段。public class Main {

public synchronized void someMethod_1() {

// Method code goes here }

public static synchronized void someMethod_2() {

// Method code goes here }

}

我们可以声明一个实例方法和一个静态方法同步。构造函数不能声明为同步。

以下代码说明了使用关键字synchronized:public class Main {

public synchronized void someMethod_1() {

// only one thread can execute here at a time

}

public void someMethod_11() {

synchronized (this) {

// only one thread can execute here at a time

}

}

public void someMethod_12() {

// multiple threads can execute here at a time

synchronized (this) {

// only one thread can execute here at a time

}

// multiple threads can execute here at a time

}

public static synchronized void someMethod_2() {

// only one thread can execute here at a time

}

public static void someMethod_21() {

synchronized (Main.class) {

// only one thread can execute here at a time

}

}

public static void someMethod_22() {

// multiple threads can execute here at a time

synchronized (Main.class) {

// only one thread can execute here at a time

}

// multiple threads can execute here at a time

}

}

wait()方法

对wait()方法的调用必须放在synchronized方法或同步块中。

对于当前线程已经获取监视器的对象,必须调用wait()方法。

notify()方法

没有办法唤醒等待集中的特定线程。

例子public class Main {

private static int myValue = 1;

public static void main(String[] args) {

Thread t = new Thread(() -> {

while (true) {

updateBalance();

}

});

t.start();

t = new Thread(() -> {

while (true) {

monitorBalance();

}

});

t.start();

}

public static synchronized void updateBalance() {

System.out.println("start:" + myValue);

myValue = myValue + 1;

myValue = myValue - 1;

System.out.println("end:" + myValue);

}

public static synchronized void monitorBalance() {

int b = myValue;

if (b != 1) {

System.out.println("Balance changed: " + b);

System.exit(1);

}

}

}

上面的代码生成以下结果。

4dbf5605fc83eebb6563e2c4b427fcae.png

例2

以下代码显示了上述代码的非同步版本。public class Main {

private static int myValue = 1;

public static void main(String[] args) {

Thread t = new Thread(() -> {

while (true) {

updateBalance();

}

});

t.start();

t = new Thread(() -> {

while (true) {

monitorBalance();

}

});

t.start();

}

public static void updateBalance() {

System.out.println("start:" + myValue);

myValue = myValue + 1;

myValue = myValue - 1;

System.out.println("end:" + myValue);

}

public static synchronized void monitorBalance() {

int b = myValue;

if (b != 1) {

System.out.println("Balance changed: " + b);

System.exit(1);

}

}

}

上面的代码生成以下结果。

d4b74ae3d2265efba42b169d99feaca7.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值