【Java27】线程练习

文章目录


1.奇偶数

/*
 * 单链表节点:
 * class Node{
 * 	 Object data;
 * 	 Node next;//下一个节点
 * }
 * 双链表节点:
 * class Node{
 * 	 Node pre;//前节点
 * 	 Object data;
 * 	 Node next;//后节点
 * }
 * 二叉树节点:
 * class TreeNode{
 * 	 TreeNode parent;父节点
 * 	 Object data;
 * 	 TreeNode left;//左节点
 * 	 TreeNode right;//右节点
 * }
 */
package com.atguigu.test02.homework01;
// 案例:在子线程中输出1-100之间的偶数,主线程输出1-100之间的奇数。
public class Test01 {
    
    
	public static void main(String[] args) {
    
    
		new Thread(){
    
    
			public void run(){
    
    
				for (int i = 0; i <= 100; i+=2) {
    
    
					System.out.println("子线程:" + i);
				}
			}
		}.start();		
		for (int i = 1; i <= 100; i+=2) {
    
    
			System.out.println("主线程:" + i);
		}
	}
}
package com.atguigu.test03.homework02;
/*
 * 案例:创建和启动2个子线程,一个打印1-10之间奇数,一个打印1-10之间偶数,
(1)要求每个线程要么不打印,要么就连续打印5个数,每个数打印间隔500毫秒
(2)但两个线程不要求交替打印。
*/
public class Test02 {
    
    
	public static void main(String[] args) {
    
    
		Odd o = new Odd();
		Even e = new Even();		
		o.start();
		e.start();
	}
}
class Odd extends Thread{
    
    
	private int num = 1;
	public void run(){
    
    
		while(true){
    
    
			synchronized (Thread.class) {
    
    
				for (int i = 1; i <=5; i++) {
    
    
					System.out.println("奇数线程,第"  + i + "个:" + num);
					num += 2;
					try {
    
    
						Thread.sleep(500);
					} catch (InterruptedException e) {
    
    
						e.printStackTrace();
					}
				}
			}			
		}
	}
}
class Even extends Thread{
    
    
	private int num = 0;
	public void run(){
    
    
		while(true){
    
    
			synchronized (Thread.class) {
    
    
				for (int i = 1; i<=5; i++) {
    
    
					System.out.println("偶数线程,第"  + i + "个:" + num);
					num += 2;
					try {
    
    
						Thread.sleep(500);
					} catch (InterruptedException e) {
    
    
						e.printStackTrace();
					}
				}
			}
		}
	}
}
package com.atguigu.test05.homework03.copy;
/*
 * 案例:创建和启动2个子线程,一个打印奇数,一个打印偶数,
(1)要求实现交替打印。(2)每个数打印间隔1秒
*/
public class Test03 {
    
    
	public static void main(String[] args) {
    
    
		MyThread t1 = new MyThread();
		MyThread t2 = new MyThread();
		
		t1.start();
		t2.start();
	}
}
class PrintNumber{
    
    
	private static int num = 1;	
	public static synchronized void print(){
    
    
		try {
    
    
			PrintNumber.class.notify();
			System.out.println(Thread.currentThread().getName() + ":" + num);
			num++;
			PrintNumber.class.wait();
		} catch (InterruptedException e) {
    
    
			e.printStackTrace();
		}
	}
}
class MyThread extends Thread{
    
    
	public void run(){
    
    
		while(true){
    
    
			PrintNumber.print();
			try {
    
    
				Thread.sleep(1000);
			} catch (InterruptedException e) {
    
    
				e.printStackTrace();
			}
		}
	}
}

在这里插入图片描述

2.账户类

package com.atguigu.test06.homework04;
/*
  案例:1、创建一个银行账户类:(1)属性:账号,余额。(2)get/set。(3)toString():返回:账户:xxx,余额:xxx
  2、创建一个丈夫类:负责往里存钱,每次存款[0,10000)以内不等
  3、创建一个妻子类:负责取钱,每次取款[0,10000)以内不等,如果余额不足,要等丈夫存够了才能取
*/
public class Test04 {
    
    
	public static void main(String[] args) {
    
    
		Account a = new Account("1122", 0);
		AccountManager am = new AccountManager(a);
		Husband h = new Husband("崔志恒",am);
		Wife w = new Wife("甄玉禄",am);		
		h.start();
		w.start();
	}
}

//11111111111111111111111111111111111111111111111111111111111111111111111
class Husband extends Thread{
    
    
	private AccountManager am;
	public Husband(String name,AccountManager am) {
    
    
		super(name);
		this.am = am;
	}
	public void run(){
    
    
		while(true){
    
    
			am.save();
			try {
    
    
				Thread.sleep(5000);
			} catch (InterruptedException e) {
    
    
				e.printStackTrace();
			}
		}
	}
}

//11111111111111111111111111111111111111111111111111111111111111111111111
class Wife extends Thread{
    
    
	private AccountManager am;
	public Wife(String name,AccountManager am) {
    
    
		super(name);
		this.am = am;
	}
	public void run(){
    
    
		while(true){
    
    
			am.withdraw();
			try {
    
    
				Thread.sleep(5000);
			} catch (InterruptedException e) {
    
    
				e.printStackTrace();
			}
		}
	}
}

//11111111111111111111111111111111111111111111111111111111111111111111111
class AccountManager{
    
    
	private Account account;	
	public AccountManager(Account account) {
    
    
		super();
		this.account = account;
	}
	public synchronized void save(){
    
    
		double money = Math.random() * 10000;
		System.out.println(Thread.currentThread().getName() + "开始存钱,目前账户状态:" + account);
		System.out.println("本次存钱的数量是:" + money);
		account.setBalance(account.getBalance() + money);
		System.out.println(Thread.currentThread().getName() +  "存钱结束,目前账户状态: " + account);
		this.notify();
	}	
	public synchronized void withdraw(){
    
    
		double money = Math.random() * 10000;
		System.out.println(Thread.currentThread().getName() + "开始取钱,目前账户状态:" + account);
		while(money > account.getBalance()){
    
    
			try {
    
    
				System.out.println("本次想取钱的数量是:" + money + ",余额不足....");
				this.wait();
			} catch (InterruptedException e) {
    
    
				e.printStackTrace();
			}
		}
		account.setBalance(account.getBalance() - money);
		System.out.println(Thread.currentThread().getName() +  "取钱结束,目前账户状态: " + account);
	}
}

//111111111111111111111111111111111111111111111111111111111111111111111111
class Account{
    
    
	private String id;
	private double balance;
	public Account(String id, double balance) {
    
    
		super();
		this.id = id;
		this.balance = balance;
	}
	public Account() {
    
    
		super();
	}
	public String getId() {
    
    
		return id;
	}
	public void setId(String id) {
    
    
		this.id = id;
	}
	public double getBalance() {
    
    
		return balance;
	}
	public void setBalance(double balance) {
    
    
		this.balance = balance;
	}
	@Override
	public String toString() {
    
    
		//账户:xxx,余额:xxx
		return "账户: " + id +"余额:" + balance;
	}	
}

如下不会停止
在这里插入图片描述

package com.atguigu.test07.homework05;
/*
   案例:请按要求编写多线程应用程序,模拟多个人通过一个山洞:
​	1、这个山洞每次只能通过一个人,每个人通过山洞的时间为5秒;
​	2、随机生成10个人,同时准备过此山洞
​	3、定义一个变量用于记录通过隧道的人数
​	4、显示每次通过山洞人的姓名,和通过顺序;
 */
public class Test05 {
    
    
	public static void main(String[] args) {
    
    
		Tunnel t = new Tunnel();		
		for (int i = 1; i <= 10; i++) {
    
    
			Thread th = new Thread(t, "p" + i);
			th.start();
		}
	}
}

//111111111111111111111111111111111111111111111111111111111111111111111111111
class Tunnel implements Runnable{
    
    
	private int num;	
	public void run(){
    
    
		cross();
	}	
	//每次只能由一个线程执行,相当于是一个人通过
	public synchronized void cross(){
    
    
		System.out.println(Thread.currentThread().getName() + "准备开始通过...");
		try {
    
    
			Thread.sleep(5000);
		} catch (InterruptedException e) {
    
    
			e.printStackTrace();
		}
		num++;
		System.out.println(Thread.currentThread().getName()+"通过了隧道,TA是第" + num);
	}
}

在这里插入图片描述
B站/知乎/微信公众号:码农编程录
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43435675/article/details/112649509