java多线程之线程同步

本文通过示例代码展示了Java中多线程并发执行时可能出现的线程不同步问题及解决方法。通过使用synchronized关键字确保了Account实例的原子性,避免了资源竞争导致的数据不一致。

java中的多线程是并发执行的,如果两个不同的线程之间访问同一资源(类实例变量、文件系统、网络资源等)的话,则有可能出现线程不同步的问题,以如下代码为例。

public void testSynchronized(){
        Account account 
= new Account();
        
//创建一个新线程 命名为thread1
        Thread thread1 = new Thread(new AccountThread(account),"thread1");
        thread1.start();
        
//创建另外一个新线程 命名为thread2
        Thread thread2 = new Thread(new AccountThread(account),"thread2");
        thread2.start();
    }


    
class AccountThread implements Runnable {
        
private Account account;
        
public AccountThread(Account account){
            
this.account = account;
        }

        
public void run() {
            account.deduct();
        }

    }


    
class Account{
        
float total = 100000000;
        
void deduct(){
            System.out.println(Thread.currentThread().getName() 
+ " deduct start");
            
try {
                
//让当前线程暂时休眠 可更明显观察到效果
                Thread.sleep(3000);
            }
 catch (InterruptedException e) {
                e.printStackTrace();
            }

            total 
-= 50000000;
            System.out.println(Thread.currentThread().getName() 
+ " deduct end");
        }

    }


    
public static void main(String[] args){
        Main main 
= new Main();
        main.testSynchronized();
    }

运行结果如下:
thread1 deduct start
thread2 deduct start
thread1 deduct end
thread2 deduct end
从结果中可以很明显看出,thread1和thread2是并发执行的,他们“同时”在调用Account实例中的deduct方法,在一些应用中,这种情况是不允许出现的,为了保证Account实例的原子性(Atomic),需要将Account类中的deduct方法标识为synchronized,如下:

synchronized void deduct(){
...........
}

修改后,运行结果如下:

thread1 deduct start
thread1 deduct end
thread2 deduct start
thread2 deduct end
从结果中可以很明显看出,thread1在调用deduct方法时,thread2只能处于等待状态,只有等到thread1执行完deduct方法,并释放了对该资源的同步锁后,thread2才能有机会开始执行deduct方法。
上面的结果还说明了一个问题:当线程处于休眠状态时(调用了sleep方法后),并不会对该线程占有的资源锁进行释放。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值