第七章

博客内容包含一系列选项,有A、B、C等选项,如(1)选A,(2)选B等。

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

1>(1)A (2) B (3)C (4)C (5)A (6)C
4>


public class word4_1 {
	public static void main(String[] args) {
		Mythread t = new Mythread();
        t.start();
	}
}

class Mythread extends Thread{
	public void run(){
		for(int i=1;i<=5;i++){
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}
}


public class word4_2 {
	public static void main(String[] args) {
		Thread t = new Thread(new Mythread2());
        t.start();
	}
}

class Mythread2 implements Runnable{
	public void run(){
		for(int i=1;i<=5;i++){
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}
}

5>


public class word5_1 {
	public static void main(String[] args) {
		word5_2 word=new word5_2();
	    Thread one=new Thread(word);
	    Thread two=new Thread(word);
	    one.setName("张三");
	    two.setName("张三的妻子");
	    one.start();
	    two.start();
	}
}


public class word5_2 implements Runnable{

	private word5_3 word = new word5_3();
	
	@Override
	public void run() {
		for (int i = 0; i < 5; i++) {
			makeWithdrawal(100);
			if (word.getbalance() < 0) {
				System.out.println("账户透支了!");
			}
		}
	}

	private synchronized void makeWithdrawal(int num) {
		if(word.getbalance()>num){
			System.out.println(Thread.currentThread().getName() + " 准备取款");
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}else{
			System.out.println("余额不足以支付"+Thread.currentThread().getName()+"的余额为"+word.getbalance());
		}
	}
}


public class word5_3 {
	
	private int balance = 1000;
	
	public int getbalance() {
		return balance;
	}
	public void Calculation(int num){
		balance = balance-num;
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值