标签:ide void run sys zed object 查询 count() 分享
idea
线程安全问题产生于多个线程同时访问共享资源(通常查询不会产生)
假如我现在想讲一个数循化加一,最终增加到1000.但是需要用5个线程来加
class Count implements Runnable{ private int count=1; public void run() { while (count<=1000){ count=count+1; System.out.println(Thread.currentThread().getName()+",count:"+count); } } } public class ThreadTest { public static void main(String[] args) { Count count=new Count(); Thread t1=new Thread(count,"线程一"); Thread t2=new Thread(count,"线程二"); Thread t3=new Thread(count,"线程三"); Thread t4=new Thread(count,"线程四"); Thread t5=new Thread(count,"线程五"); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); } }
结果:
代码显示:最多会增加到1000循环就会结束那么为什么会出现1001呢!!
由于现在是多线程增加,有可能当count增加到999的时候同时又两个线程都进入了while循环里,然后就连续增加了两次
那么怎么解决呢!!!
class Count implements Runnable{ private volatile int count=1; private static Object oj = new Object(); public void run() { while (count<=1000){ synchronized (oj) {//oj就是锁对象可以为任意对象也可为this if(count<=1000) { count = count + 1; System.out.println(Thread.currentThread().getName() + ",count:" + count); } } } } } public class ThreadTest { public static void main(String[] args) { Count count=new Count(); Thread t1=new Thread(count,"线程一"); Thread t2=new Thread(count,"线程二"); Thread t3=new Thread(count,"线程三"); Thread t4=new Thread(count,"线程四"); Thread t5=new Thread(count,"线程五"); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); } }
class Count implements Runnable{ private volatile int count=1; private static Object oj = new Object(); public void run() { while (count<=1000){ sole(); } } public synchronized void sole(){//这把锁的锁对象就是this if(count<=1000) { count = count + 1; System.out.println(Thread.currentThread().getName() + ",count:" + count); } } } public class ThreadTest { public static void main(String[] args) { Count count=new Count(); Thread t1=new Thread(count,"线程一"); Thread t2=new Thread(count,"线程二"); Thread t3=new Thread(count,"线程三"); Thread t4=new Thread(count,"线程四"); Thread t5=new Thread(count,"线程五"); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); } }
标签:ide void run sys zed object 查询 count() 分享
原文地址:https://www.cnblogs.com/wy0119/p/8999163.html