Java 多线程

本文深入探讨了Java多线程的实现方式,包括线程创建、线程间通信、线程同步、等待与唤醒机制等核心概念。通过具体示例,如存款、取款操作的线程安全问题,演示了如何使用synchronized关键字、wait和notifyAll方法来确保数据的一致性和线程间的正确交互。

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

简单例子:

package 多线程;

public class Thread1 implements Runnable{

	@Override
	public void run() {
		// TODO 自动生成的方法存根
		for(int i=0;i<10;i++){
			System.out.println(Thread.currentThread().getName()+" "+i);
		}
	}
	
}
package 多线程;

public class Test1 {
	
	public static void main(String[] args) {
		Thread1 thread1 = new Thread1();
		Thread1 thread2 = new Thread1();
		Thread thread = new Thread(thread1);
		Thread threads = new Thread(thread2);
		thread.start();
		threads.start();
		
	}
}

 

重写star 

package 多线程;

public class ThreadTest implements Runnable{

	private Thread thread;
	private String threadName;
	public void run() {
		// TODO 自动生成的方法存根
		for(int i = 0;i<1000;i++){
			System.out.println(threadName+"  "+i);
			try {
				thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

	

	public ThreadTest(String threadName) {
		super();
		this.threadName = threadName;
	}



	public void setThreadName(String threadName) {
		this.threadName = threadName;
	}

	public void start() {
		// TODO 自动生成的方法存根
		if(thread == null){
			thread= new Thread(this,threadName);
			thread.start();
		}
	}

}
package 多线程;

public class Test {
	public static void main(String[] args) {
		ThreadTest thread = new ThreadTest("强月城");
		thread.start();
		ThreadTest thread1 = new ThreadTest("优快云");
		thread1.start();
	}
}

 

优快云  0
强月城  0
强月城  1
优快云  1
强月城  2
优快云  2
强月城  3
优快云  3
强月城  4
优快云  4。。。。。

 

join 等待完成

package 多线程join;

public class Thread_join implements Runnable {
	@Override
	public void run() {
		// TODO 自动生成的方法存根
		for (int i = 0; i < 10; i++) {

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

}
package 多线程join;

public class Demo {
	public static void main(String[] args) throws InterruptedException {
		Thread_join thread_join1 = new Thread_join();
		Thread thread1 = new Thread(thread_join1);
		thread1.start();
		for(int i = 0;i<10;i++){
			System.out.println("main	"+i);
			if(i == 5){
				thread1.join();
			}
		}
	}
}

wait 等待    notifyAll 唤醒    synchronized同步 

package 多线程wait_notifyAll_synchronized;

public class Account {
	//编号,余额
	private String accountNo;
	private double balance;
	private boolean flag = false;
	public String getAccountNo() {
		return accountNo;
	}
	public double getBalance() {
		return balance;
	}
	public Account(String accountNo, double balance) {
		this.accountNo = accountNo;
		this.balance = balance;
	}
	public synchronized void draw(double drawAmount) {
		if(!flag){
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}else {
			if(balance-drawAmount>=0){
				System.out.println(Thread.currentThread().getName()+"取钱"+drawAmount);
				balance = balance-drawAmount;
				System.out.println("余额为:"+balance);
			}
			flag = false;
			notifyAll();
		}
	}
	
	public synchronized void deposit(double depositAmount) {
		if(flag){
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}else {
			System.out.println(Thread.currentThread().getName()+"存钱"+depositAmount);
			balance = balance+depositAmount;
			System.out.println("余额为:"+balance);
				flag = true;
				notifyAll();
		}
	}
}
package 多线程wait_notifyAll_synchronized;

public class DrawThread implements Runnable{
	private Account account ;
	private double drawAmount;
	
	public DrawThread(Account account, double drawAmount) {
		super();
		this.account = account;
		this.drawAmount = drawAmount;
	}

	@Override
	public void run() {
		// TODO 自动生成的方法存根
		for(int i =0;i<10;i++){
			account.draw(drawAmount);
		}
	}
	
}
package 多线程wait_notifyAll_synchronized;

public class DepositThread implements Runnable{
	private Account account ;
	private double depositAccount;
	
	public DepositThread(Account account, double depositAccount) {
		super();
		this.account = account;
		this.depositAccount = depositAccount;
	}

	@Override
	public void run() {
		// TODO 自动生成的方法存根
		for(int i =0;i<10;i++){
			account.deposit(depositAccount);
		}
	}
}
package 多线程wait_notifyAll_synchronized;

public class Demo {
	public static void main(String[] args) {
		Account account = new Account("qyc", 0);
		DrawThread drawThread = new DrawThread(account, 700);
		DepositThread depositThread  = new DepositThread(account, 600);
		Thread thread = new Thread(drawThread);
		Thread thread2 = new Thread(depositThread);
		thread.start();
		thread2.start();
	}
	
	
}

Thread-1存钱600.0
余额为:600.0
Thread-1存钱600.0
余额为:1200.0
Thread-0取钱700.0
余额为:500.0
Thread-1存钱600.0
余额为:1100.0
Thread-0取钱700.0
余额为:400.0
Thread-1存钱600.0
余额为:1000.0
Thread-0取钱700.0
余额为:300.0
Thread-1存钱600.0
余额为:900.0
Thread-0取钱700.0
余额为:200.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值