java 多线程

在这里插入图片描述
在这里插入图片描述

join

public class Test {
	
	/*
	 * 线程调度方法
	 *   join:等待该线程终止,可以在一定概率上保证线程的公平性
	 */
	public static void main(String[] args) {
		
		//1:创建2个线程
		ThreadJoin tj1 = new ThreadJoin();
		ThreadJoin tj2 = new ThreadJoin();
		ThreadJoin tj3 = new ThreadJoin();
		//2:线程起名
		tj1.setName("强");
		tj2.setName("马");
		tj3.setName("喆");
		//3:启动线程
		tj1.start();
		try {
			tj1.join(); //等待该线程执行完毕
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		tj2.start();
		tj3.start();
		
	}

}

public class ThreadJoin extends Thread {
	
	@Override
	public void run() {
		
		for(int i =1;i<=100;i++){
			
				System.out.println(this.getName()+":"+i);
				//this.join();//等待该线程终止
				//this.join(100L);
			
		}
	}	
}

sleep

/*
* sleep:
* 使线程停止运行一段时间,将处于阻塞状态
* 如果调用了sleep方法之后,没有其他等待执行的线程,这个时候当前线程不会马上恢复执行!
*
*/

public class Test {
	

	public static void main(String[] args) {

		//1:创建2个线程
		ThreadSleep ts1 = new ThreadSleep();
		ThreadSleep ts2 = new ThreadSleep();
		//2:线程起名
		ts1.setName("宝强");
		ts2.setName("马蓉");
		//3:启动线程
		ts1.start();
		ts2.start();
	}

}


import java.util.Date;

public class ThreadSleep extends Thread {

	@Override
	public void run() {
		
		for(int i =1;i<=100;i++){
			try {
				Thread.sleep(10L);
				//System.out.println(new Date());
				System.out.println(this.getName()+":"+i);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}
	}

	
}

Yield

/*
* Yield:
* 让当前正在执行线程暂停,不是阻塞线程,而是将线程转入就绪状态
* 如果调用了yield方法之后,没有其他等待执行的线程,这个时候当前线程就会马上恢复执行!

public class Test {
		public static void main(String[] args) {

		//1:创建2个线程
		ThreadYield ty1 = new ThreadYield();
		ThreadYield ty2 = new ThreadYield();
		//2:线程起名
		ty1.setName("宝");
		ty2.setName("马");
		//3:启动线程
		ty1.start();
		ty2.start();
	}

}

public class ThreadYield extends Thread {

	
	@Override
	public void run() {
		
		for(int i =1;i<=100;i++){
			this.yield();//暂停当前线程,会到就绪状态
		   System.out.println(this.getName()+":"+i);

		}
	}
}

SetDaemon

/*
* setDaemon:守护线程
* 可以将指定的线程设置成后台线程
* 创建后台线程的线程结束时,后台线程也随之消亡
* 只能在线程启动之前把它设为后台线程


public class Test {
		public static void main(String[] args) {
		
		//创建3个线程
		ThreadDaemon td1 = new ThreadDaemon();
		ThreadDaemon td2 = new ThreadDaemon();
		
		//线程起名
		td1.setName("关羽");
		td2.setName("张飞");
		
		Thread main = Thread.currentThread();
		main.setName("刘备");
		
		td1.setDaemon(true);  //守护线程
		td2.setDaemon(true);  //守护线程
		
		
		td1.start();
		td2.start();
		
		for(int i = 1;i<=5;i++){
			System.out.println(main.getName()+":"+i);
		}
		}
}

public class ThreadDaemon extends Thread {
	  @Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println(this.getName()+":"+i);
		}
	}
}

stop

  • 线程死亡
    * 1:正常死亡,程序正常执行完毕
    * 2:意外死亡,调用stop方法
    * stop会使当前线程立马死亡,不会执行后面的代码,带来安全隐患,所以这个方法过时了
    * interrupt:线程中断,替代了stop,可以保证线程后面的代码正常执行

public class Test {
			public static void main(String[] args) {
		
		ThreadStop ts = new ThreadStop();
		
		ts.setName("线程1");
		ts.start();
		//休息3秒必须我醒来,醒不来就弄死你
		try {
			Thread.sleep(3000L);
			//ts.stop();
			ts.interrupt();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}


public class ThreadStop extends Thread {
	
	@Override
	public void run() {
		
		try {
			System.out.println("开始时间:"+new Date().toLocaleString());
			//亲,我要休息10秒,不要打扰我
			Thread.sleep(10000L);
		} catch (InterruptedException e) {
			System.out.println("线程结束");
		}
		System.out.println("结束时间:"+new Date().toLocaleString());
		
		
	}

}

Demo


/*
 * 需求:账户取钱
 *    账户原本金额1000
 *       2个用户同时对一个账户做操作
 *       每一次取100,直到取到0为止
 * 
 */
public class Account extends Thread {
	private static int account = 1000;  //余额
	
	private static  String s = new String(); //锁对象
	
	@Override
	public void run() {
		
	
		synchronized (s) {
			while(account>0){
				try {
					Thread.sleep(100L);
					//每一次取100
						account = account-100;
					System.out.println("余额:"+account);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	

}

  • 问题:当多线程同时操作同一个资源的时候
    * 出现重复还有负数?
    *
    * 同步概念:当一个线程做操作的时候,其他线程等待,等我这个线程做完了,然后其他线程继续抢夺CPU,谁抢上,谁执行
    * 加锁概念:
    * 1:同步代码块 :把一段代码锁住
    * 1:资源是唯一的 static int account=1000
    * 2:锁是唯一的 static Object obj = new Object
    * 可以是任意一个引用类型的对象
    * 同步优点:安全
    * Vector,Hashtable
    * 缺点:效率低
    * ArrayList
    *
    * 2:同步方法:直接把一个方法锁住
    *
public class Demo {

	public static void main(String[] args) {
		//1:创建2个用户线程
		Account a1 = new Account();
		Account a2 = new Account();
		Account a3 = new Account();
		//2:启动线程
		a1.start();
		a2.start();
		a3.start();
	
		
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值