public class DaemonThread extends Thread {//定义后台线程的线程体与普通线程没有什么区别public void run() {for(int i=0;i<1000;i++) {System.out.println(getName()+" "+i);}}public static void main(String[] args) {DaemonThread t=new DaemonThread();//将此线程设置为后台线程t.setDaemon(true);t.start();for(int i=0;i<10;i++) {System.out.println(Thread.currentThread().getName()+" "+i);}//程序到此执行结束,前台线程(main)结束,后台线程也随之结束}}运行结果:
main 0Thread-0 0main 1Thread-0 1Thread-0 2main 2Thread-0 3Thread-0 4Thread-0 5main 3main 4Thread-0 6main 5Thread-0 7Thread-0 8main 6main 7main 8Thread-0 9main 9Thread-0 10Thread-0 11Thread-0 12Thread-0 13Thread-0 14Thread-0 15Thread-0 16Thread-0 17Thread-0 18Thread-0 19Thread-0 20Thread-0 21线程睡眠:如果需要让当前正在执行的线程暂停一段时间,并进入阻塞状态,则可以通过调用 Thread类的静态 sleep方法来实现 。sleep方法有两种重载形式
static void sleep(long millis):让当前正在执行的线程暂停millis毫秒,并进入阻塞状态
static void sleep(long millis,int nanos):让当前正在执行的线程暂停millis毫秒加上nanos毫微秒,并进入阻塞状态,通常我们不会精确到毫微秒,所以该方法不常用
import java.util.Date; public class SleepTest {public static void main(String[] args) throws InterruptedException {for(int i=0;i<10;i++) {System.out.println("当前时间"+new Date());Thread.sleep(1000);}} }改变线程优先级:每个线程执行时都有一定的优先级,优先级高的线程获得较多的执行机会,优先级低的线程则获得较少的执行机会 。
每个线程默认的优先级都与创建它的父线程的优先级相同,在默认情况下,main线程具有普通优先级,由main线程创建的子线程也具有普通优先级 。
Thread类提供了 setPriority(int newPriority)、 getPriority()方法来设置和返回指定线程的优先级,其中 setPriority()方法的参数可以是一个整数,范围是1-10之间,也可以使用 Thread类的如下三个静态常量
MAX_PRIORITY:其值是10
MIN_PRIORITY:其值时1
NORM_PRIPRITY:其值是5
public class PriorityTest extends Thread {//定义一个构造器,用于创建线程时传入线程的名称public PriorityTest(String name) {super(name);}public void run() {for(int i=0;i<50;i++) {System.out.println(getName()+",其优先级是:"+getPriority()+"循环变量的值:"+i);}}public static void main(String[] args) {//改变主线程的优先级Thread.currentThread().setPriority(6);for(int i=0;i<30;i++) {if(i==10) {PriorityTest low=new PriorityTest("低级");low.start();System.out.println("创建之初的优先级:"+low.getPriority());//设置该线程为最低优先级low.setPriority(Thread.MIN_PRIORITY);}if(i==20) {PriorityTest high=new PriorityTest("高级");high.start();System.out.println("创建之初的优先级"+high.getPriority());high.setPriority(Thread.MAX_PRIORITY);}}}}线程同步线程安全问题:现有如下代码:
public class Account {private String accountNo;private double balance;public Account() {}public Account(String accountNo, double balance) {super();this.accountNo = accountNo;this.balance = balance;}public String getAccountNo() {return accountNo;}public void setAccountNo(String accountNo) {this.accountNo = accountNo;}public double getBalance() {return balance;}public void setBalance(double balance) {this.balance = balance;}public int hashCode() {return accountNo.hashCode();}public boolean equals(Object obj) {if(this==obj) {return true;}if(obj!=null&&obj.getClass()==Account.class) {Account target=(Account)obj;return target.getAccountNo().equals(accountNo);}return false;}}import com.alibaba.util.Account; public class DrawThread extends Thread{//模拟用户账户private Account account;//当前取钱线程所希望的钱数private double drawAmount;public DrawThread(String name,Account account,double drawAmount) {super(name);this.account=account;this.drawAmount=drawAmount;}//多个线程修改同一个共享数据,可能发生线程安全问题@Overridepublic void run() {if(account.getBalance()>drawAmount) {System.out.println(getName()+"取钱成功"+" "+drawAmount);try {Thread.sleep(1);}catch(Exception e) {e.printStackTrace();}account.setBalance(account.getBalance()-drawAmount);System.out.println("t余额为"+" "+account.getBalance());}else {System.out.println("余额不足,取钱失败");}}} import com.alibaba.util.Account; public class DrawTest {public static void main(String[] args) {Account account=new Account("1234567",1000);//模拟两个线程同时操作账号new DrawThread("甲", account, 800).start();;new DrawThread("乙", account, 800).start();;} }
推荐阅读
- 唐僧取经是哪个朝代的事
- 皎然贬酒褒茶道,唐代位嗜茶的诗僧皎然介绍
- 考上清华北大的人特点是什么?
- 佛教僧侣茶俗的繁荣,四川民俗中的茶俗谚语介绍
- 高僧|唐朝一富家公子,酷爱乘船钓鱼,从不去风月之地,后成一代高僧
- 茶艺的缘起及发展,唐代位嗜茶的诗僧皎然介绍
- 机器人|扫地机器人并非万能!有些活干不了
- 能与清华北大PK的大学专业
- 茶叶的制作过程详解,严君谢僧寄茶茶叶茶诗详解
- 茶与僧侣的不解之缘,佛中有茶
