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方法后),并不会对该线程占有的资源锁进行释放。
本文通过示例代码展示了Java中多线程并发执行时可能出现的线程不同步问题及解决方法。通过使用synchronized关键字确保了Account实例的原子性,避免了资源竞争导致的数据不一致。
3323

被折叠的 条评论
为什么被折叠?



