并发this.getName()和Thread.currentThread().getName()区别

本文介绍了使用Java创建线程并实现线程运行的具体过程,包括线程对象的创建、设置线程名称、启动线程及线程状态的判断等核心操作。
package com.test.thread;

import java.util.ArrayList;
import java.util.List;

public class CountOperate extends Thread
{
    public CountOperate()
    {
        super();
        System.out.println("CountOperate---begin");
        System.out.println(Thread.currentThread().getName());
        System.out.println(Thread.currentThread().isAlive());
        System.out.println(this.getName());
        System.out.println(this.isAlive());
        System.out.println("CountOperate---end");
    }

    @Override
    public void run()
    {
        System.out.println("run---begin");
        System.out.println(Thread.currentThread().getName());
        System.out.println(Thread.currentThread().isAlive());
        System.out.println(this.getName());
        System.out.println(this.isAlive());
        System.out.println("run---end");
    }

    public static void main(String[] args)
    {
        CountOperate countOperate = new CountOperate();
        Thread t1 = new Thread(countOperate);
        t1.setName("A");
        t1.start();
        System.out.println(t1.isAlive());
    }
}

输出结果:

CountOperate—begin
main
true
Thread-0
false
CountOperate—end
true
run—begin
A
true
Thread-0
false
run—end

首先要清楚thread和t1是两个完全不同的对象,他俩之间唯一的关系就是把thread传递给t1对象仅仅是为了让t1调用thread对象的run方法。hello thread = new hello();运行这句话的时候会调用hello的构造方法,Thread.currentThread().getName()是获得调用这个方法的线程的名字,在main线程中调用的当然是main了,而this.getName()这个方法是获取当前hello对象的名字,只是单纯的方法的调用。因为没有重写这个方法所以调用的是父类Thread(把这个对象当作是普通的对象)中的方法。
后面的输出类似。t1.setName(“A”);这句话只是修改了t1的名字,和thread对象没有关系,所以run方法中this.getName()的输出还是Thread-0。

package itbaizhan; /** * 银行取钱案例 -》两个用户同时对一个账户取钱,出现线程冲突问题 */ public class DrawMoneyThreadTest { public static void main(String[] args) { Account account = new Account("chenbin", "mima", 1000); new Thread(new drawMoneyThread(account, 900), "老公").start(); new Thread(new drawMoneyThread(account, 900), "老婆").start(); } } /** * 取钱线程 */ class drawMoneyThread implements Runnable { private Account account; //用户账户 private double drawMoney; //取钱金额 public drawMoneyThread(Account account, double drawMoney) { this.account = account; this.drawMoney = drawMoney; } @Override public void run() { synchronized (this.account) { if (account.getBalance() >= drawMoney) { System.out.println(Thread.currentThread().getName() + " 取钱成功,吐出钞票 " + this.drawMoney); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } this.account.setBalance(this.account.getBalance() - this.drawMoney); System.out.println(Thread.currentThread().getName() + " 剩余金额: " + this.account.getBalance()); } else { System.out.println(Thread.currentThread().getName() + " 取钱失败,剩余金额不足"); } } } } /** * 账户类 */ class Account { private String AccountId; private String MiMa; private double balance;//余额 public String getAccountId() { return AccountId; } public void setAccountId(String accountId) { AccountId = accountId; } public String getMiMa() { return MiMa; } public void setMiMa(String miMa) { MiMa = miMa; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public Account(String accountId, String miMa, double balance) { AccountId = accountId; MiMa = miMa; this.balance = balance; } public Account() { } } 为什么这个锁没有同步
最新发布
10-31
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值