java夯实基础-多线程

本文总结java中多线程

1、线程的创建

package com.wllfengshu.test;

import org.junit.Test;
/**
 * 线程的创建Thread
 * 线程四种状态:新建、运行、中断、死亡
 */
public class Test1线程创建 {
	//线程创建
	@Test
	public void test1(){
		A a=new A();
		Thread b=new Thread(new B());
		a.start();
		b.start();
		while(true){//主线程也在死循环
			System.out.println("Main");
		}
	}
	//线程常用方法
	@Test
	public void test2(){
		A a=new A();
		System.out.println(a.isAlive());//线程是否在运行状态
		a.start();//开启线程
		System.out.println(a.getName());//获取线程名族
		while(true){//主线程也在死循环
			System.out.println("Main");
			try {
				Thread.sleep(1000);//睡觉
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("此时占用CUP的线程是:"+Thread.currentThread());//此时占用CUP的线程
		}
		
	}
}
class A extends Thread{//继承Thread类
	@Override
	public void run() {
		while(true){
			try {
				Thread.sleep(1000);//睡觉
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(" A ");
		}
	}
}
class B implements Runnable{//实现Runnable接口
	@Override
	public void run() {
		while(true){
			System.out.println(" B ");
		}
	}
}
2、线程同步
package com.wllfengshu.test;

import org.junit.Test;

public class Test2线程同步 {
	@Test
	public void test1(){
		Bank bank=new Bank();
		Thread kuaiji=new Thread(bank);
		kuaiji.setName("会计");
		Thread chuna=new Thread(bank);
		chuna.setName("出纳");
		kuaiji.start();
		chuna.start();
	}
}
class Bank implements Runnable{
	private int money=0;
	public synchronized void addOrSub(int n){
		if (Thread.currentThread().getName().equals("会计")) {
			for (int i = 0; i < 3; i++) {
				money+=n; 
				System.out.println(Thread.currentThread().getName()+"做了加法运算"+money+",睡觉了");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}else if (Thread.currentThread().getName().equals("出纳")){
			for (int i = 0; i < 3; i++) {
				money-=n;
				System.out.println(Thread.currentThread().getName()+"做了减法运算"+money+",睡觉了");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}	
	}
	@Override
	public void run() {
		if (Thread.currentThread().getName().equals("会计")) {
			addOrSub(8);//加
		}else if (Thread.currentThread().getName().equals("出纳")){
			addOrSub(5);//减
		}	
	}
}
3、Wait和notify
package com.wllfengshu.test;

public class Test3Wait和notify {
	//售票员没有零钱    每张票5元
	//张一用一张10的去买票,等
	//张二用一张5元的买票,问题解决
	public static void main(String[] args) {
		Tick tick=new Tick();
		Thread zhangOne= new Thread(tick);
		zhangOne.setName("张一");
		Thread zhangTwo= new Thread(tick);
		zhangTwo.setName("张二");
		zhangTwo.start();
		zhangOne.start();
	}
}
class Tick implements Runnable{
	private int saler=0;//售票员
	public synchronized void saleTick(int money){
		if (money==5) {
			System.out.println(Thread.currentThread().getName()+"的钱正好,给他卖票了");
			saler+=5;
		}else if(money==10){
			while(saler<5){
				try {
					System.out.println(Thread.currentThread().getName()+"给的10元,不能买票,等待");
					wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			saler+=10;
			System.out.println(Thread.currentThread().getName()+"给他卖票了");
		}
		notifyAll();
	}
	@Override
	public void run() {
		if (Thread.currentThread().getName().equals("张一")) {
			saleTick(5);
		}else if (Thread.currentThread().getName().equals("张二")){
			saleTick(10);
		}
	}
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值