java基础学习日志17

多线程

线程:在一个程序中同时运行的多个独立流程,每一个独立的流程就是一个线程。两种实现方式:继承Thread类与实现 Runnable接口



public class Runthread extends Thread {

	String name;
	int num;

	public Runthread(String name, int num) {
		this.name = name;
		this.num = num;
	}

	public void run() {

		int number = 1000;
		Thread.currentThread().setName(name);
		for (int i = 0; i < number; i++) {
			System.out.println(Thread.currentThread().getName() + "组的" + (i + 1) + "号选手接棒");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int j = 0;
			for (j = 0; j < num;) {
				try {
					Thread.sleep(1300);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				j += 50;
				number -= 50;
				System.out.println(
						Thread.currentThread().getName() + "组\t" + (i + 1) + "号选手跑了\t" + j + "米" + "还剩" + number);
			}
			System.out.println(Thread.currentThread().getName() + "组的" + (i + 1) + "号选手完成了他的任务");
			if (i == 0 && j == 0) {
				System.out.println(Thread.currentThread().getName() + "组跑完了");
			}
		}

	}

	public static void main(String[] args) {
		Runthread p1 = new Runthread("A", 250);
		Runthread p2 = new Runthread("B", 250);
		p1.start();
		p2.start();
	}

}




public class MyRunnable implements Runnable {

	@Override
	public void run() {
		for (int i = 0; i < 40; i++) {
			System.out.println(Thread.currentThread().getName() + ":" + i);
		}
	}

	public static void main(String[] args) {
		MyRunnable th = new MyRunnable();
		Thread thr = new Thread(th);
		MyRunnable th1 = new MyRunnable();
		Thread thr1 = new Thread(th1);
		thr.start();
		thr1.start();
	}

}


public class ThreadDemo {
	public static void main(String[] args) {
		Thread th = Thread.currentThread();
		System.out.println(th.getName());
		th.setName("你瞎啊");
		System.out.println(Thread.currentThread());
		
		
		
	}
}


public class MyThread extends Thread {

	public void run() {
		Thread th = Thread.currentThread();
		th.setName("亚索");
		for (int i = 40; i >= 0; i--) {
			System.out.println(th.getName() + "剩余血量:" + i);
			if (i == 0) {
				System.out.println(th.getName() + "挂壁了");
			}
		}
	}

	public static void main(String[] args) {
		MyThread th = new MyThread();
		MyThread th1 = new MyThread();
		th.start();
		th1.start();
	}
}


小练习

public class ClimbThread extends Thread {
	String name;
	int num;
	double time;

	public ClimbThread(String name, int num, double time) {
		this.name = name;
		this.num = num;
		this.time = time;
	}

	public void run() {
		Thread.currentThread().setName(name);
		for (int i = 0; i < num; i++) {
			double n = (Math.random() * 5);
			try {
				Thread.currentThread().sleep((long) n * 1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			time += n;
			if (i==num-1) {
				System.out.println(Thread.currentThread().getName() + "跑完了"+"\t" + "目前总计用时:" + time + "秒");
			}else {
				System.out.println(Thread.currentThread().getName() + "\t" + "爬了" +(i+1) + "米" + "\t" + "目前总计用时:" + time + "秒");

			}
		}
	}

	public static void main(String[] args) {
		ClimbThread ct = new ClimbThread("任俊成", 10, 0);
		ClimbThread ct1 = new ClimbThread("亚索", 10, 0);
		ct.start();
		ct1.start();

	}

}


public class Test_bank {
	public static void main(String[] args) {
		Card card = new Card(8000);

		WithdrawThread thread2 = new WithdrawThread(card, 200);
		DepositThread thread1 = new DepositThread(card, 1000);
		// thread1.setThread(thread2);
		// thread2.setThread(thread1);

		thread1.start();
		thread2.start();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("最终金额是:" + card.getBalance());
	}

}

class Card {
	private double balance;
	private Object lock = new Object();
	private Object lock1 = new Object();

	public Card(double balance) {
		this.balance = balance;
	}

	public double getBalance() {
		return this.balance;
	}

	/* 存款 */
	public synchronized void deposit(double count) {
		System.out.println("存钱--线程:存入金额=" + count);
//		synchronized (lock) {
		while(true){
			//如果条件你不满足,则等待条件,并且是方法锁,如果条件满足,则break继续
			if (balance+count>12000) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}else{
				break;
			}
			double now = balance + count;
			try {
				Thread.sleep(100);//存钱操作用时0.1秒钟
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
//			synchronized (lock1) {
				balance = now;
				this.notifyAll();//考虑的条件(余额)有变,唤醒所有等待条件的线程
				System.out.println("存钱--线程:当前金额=" + balance);
//			}
		}
//		}

	}

	/* 取款 */
	public synchronized void withdraw(double count) {
		System.out.println("取钱--线程:取出金额=" + count);
		while (true) {
			//如果条件不满足,则等待条件,并且释放锁;如果条件满足,则break继续
			if (balance-count<5000) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}else {
				break;
			}
		}
		
		double now = balance - count;
//		synchronized (lock) {
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
//			synchronized (lock1) {
				balance = now;
				this.notifyAll();
				System.out.println("取钱--线程:当前金额=" + balance);
//			}

//		}

	}

}

// 存钱
class DepositThread extends Thread {
	private Card card;
	private WithdrawThread withdrawThread;
	private final long SLEEP_TIME;// 执行一次存钱后休眠的时间

	public DepositThread(Card card, long sleeptime) {
		this.card = card;
		this.SLEEP_TIME = sleeptime;
	}

	// public DepositThread(Card card) {
	// this.card = card;
	// }

	public void setThread(WithdrawThread thread) {
		this.withdrawThread = thread;
	}

	@Override
	public void run() {
		System.out.println("存钱窗口:来啊,快了存钱啊");
		try {
			// withdrawThread.join();
			// Thread.sleep(100);
			while (true) {// 每隔SLEEP_TIME时间就循环一次存钱操作
				card.deposit(1000);
				Thread.sleep(SLEEP_TIME);
			}

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("存钱窗口:存完了");

	}

}

// 取钱
class WithdrawThread extends Thread {
	private Card card;
	private DepositThread depositThread;
	private final long SLEEP_TIME;// 执行一次取钱后休眠的时间

	public WithdrawThread(Card card, long sleeptime) {
		this.card = card;
		this.SLEEP_TIME = sleeptime;
	}

	// public WithdrawThread(Card card) {
	// this.card = card;
	// }

	public void setThread(DepositThread thread) {
		this.depositThread = thread;
	}

	public void run() {
		System.out.println("取钱窗口:我要拿钱了");

		try {
			// 为了让存钱线程先执行,这里先睡眠一下
			// Thread.sleep(10);
			while (true) {// 每隔SLEEP_TIME时间就循环一次取钱操作
//				Thread.sleep(200);
				card.withdraw(500);
				Thread.sleep(SLEEP_TIME);
			}
			// depositThread.join(1000);
			// Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("钱到手了");

	}

}


public class Runnables implements Runnable{
	private int meters = 1000;

	public void run() {
		while (true) {
			synchronized (this) {
				if (meters <= 100) {
					break;
				}

				System.out.println(Thread.currentThread().getName() + "拿到接力棒!");
				for (int i = 0; i < 100; i += 10) {
					try {
						Thread.sleep(600);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName() + "跑了" + (i + 10) + "米!");
				}
				meters -= 100;
			}
		}
	}
}




import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Runnables run = new Runnables();
		for(int i=0;i<4;i++){
			new Thread(run,(i+1)+"号选手").start();
		}
	}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值