马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 可爱的静静 于 2017-8-22 14:50 编辑
多线程安全问题 线程运行状态
看图一啊啊啊 啊啊啊啊 啊 啊 在最后面 哈哈哈哈哈 打错字了我 是清亡 1. Sleep(time) 也就是休眠的状态 本来有执行去哪执行资格了,但是休眠一段时间后,时间到了 就再有执行资格执行权就是运行的状态,但是如果在它休眠的时候,执行权被其他线程抢了,就变成是临时状态了,有执行走个没有执行权 2 就是你有执行资格,但是抢不到cpu执行权 其他线程在执行的话就是临时状态了 3 就是线程结束了 当run方法执行完就结束啦 或者是stop()方法 例子: - package UI;
-
- import java.lang.Thread;
-
- public class sport {
- public static void main(String[] args) {
-
- Ticket a=new Ticket();
- Ticket b=new Ticket();
- Thread b1 =new Thread(a);
- Thread b2 =new Thread(a);
- b1.start();
- try { Thread.sleep(10);} catch (Exception e){};
- a.flag=false;
- b2.start();
- }
-
- }
- class Ticket implements Runnable
- {
- private int ticket=100;
- boolean flag =true;
- Object obj =new Object();
- public void run(){
- if(flag){
- while(true){
- synchronized(this){//代码块
- if(ticket>0){
- try { Thread.sleep(10);} catch (Exception e){};
- System.out.println(Thread.currentThread().getName()+"Ticket---:"+ticket--);
- }
- }
- }
- }
- else
- while(true)
- show();
- }
- public synchronized void show(){//同步函数
- if(ticket>0)
- if(ticket>0){
- try { Thread.sleep(10);} catch (Exception e){};
- System.out.println(Thread.currentThread().getName()+"…sale:"+ticket--);
- }
- }
- }
-
复制代码
假设:
看图二啊啊啊 啊 在最后面 所以当线程2执行完if语句判断后,线程1就参与进来,执行,当线程重新执行就不再做判断直接输出 无论是不是大于 0 然后共享数据 a-1了 所以程序执行完后 输出结果有可能有-1 有0等错误数据 举个例子:(让执行判断if()判断之后睡眠 被其他线程参与 看看有什么后果) 结果问题:明明是只卖100张票可是结果是有0 1 -1 这些就是错误信息 问题原因: 当多条语句在操作一个线程共有数据时,一个线程对多条语句值执行了一部分,还没有执行完,另一个线程参与进来执行,导致共享线程数据错误 解决办法: 对多条操作共享数据的语句,只能让一个线程执行完,在执行过程中,其他线程不能参与 用代码同步就可以解决这个问题了 synchronired(对象) 同步数据后的代码块: - package UI;
-
- import java.lang.Thread;
-
- public class sport {
- public static void main(String[] args) {
-
- Ticket a=new Ticket();
- Thread b1 =new Thread(a);
- Thread b2 =new Thread(a);
- b1.start();
- try {Thread.sleep(2);} catch (Exception e){};
- /*睡眠一下
- 睡眠时间是不是被其他线程参与进来就不再判断if 对if()里面的代码是不是有影响*/
- b2.start();
-
-
- }
-
- }
- class Ticket implements Runnable
- {
- private int ticket=100;
- public void run(){
- while(true){
- if(ticket>0){
- synchronized(this){
- try {Thread.sleep(2);} catch (Exception e){};//睡眠时间 这时候线程被其他线程参与进来了
- System.out.println(Thread.currentThread().getName()+"啦啦啦:"+ticket--);
- }
- }
- }
- }
- }
复制代码
同步函数:也是用synchronired(对象) 同步函数放的代码: - package UI;
-
- import java.lang.Thread;
-
- public class sport {
- public static void main(String[] args) {
-
- Ticket a=new Ticket();
- Ticket b=new Ticket();
- Thread b1 =new Thread(a);
- Thread b2 =new Thread(a);
- b1.start();
- try { Thread.sleep(10);} catch (Exception e){};
- a.flag=false;
- b2.start();
- }
-
- }
- class Ticket implements Runnable
- {
- private int ticket=100;
- boolean flag =true;
- Object obj =new Object();
- public void run(){
- if(flag){
- while(true){
- synchronized(this){//代码块
- if(ticket>0){
- try { Thread.sleep(10);} catch (Exception e){};
- System.out.println(Thread.currentThread().getName()+"Ticket---:"+ticket--);
- }
- }
- }
- }
- else
- while(true)
- show();
- }
- public synchronized void show(){//同步函数
- if(ticket>0)
- if(ticket>0){
- try { Thread.sleep(10);} catch (Exception e){};
- System.out.println(Thread.currentThread().getName()+"…sale:"+ticket--);
- }
- }
- }
-
复制代码
|