| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2449 人关注过本帖
标题:关于多线程的demo
只看楼主 加入收藏
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
结帖率:100%
收藏
 问题点数:0 回复次数:3 
关于多线程的demo
项目结构
图片附件: 游客没有浏览图片的权限,请 登录注册

一直跑的线程
程序代码:
package com.xiaoa.thread;

public class Mythread implements Runnable {
    int tickets = 100;//線程共享的成員變量

    @Override
    public void run() {
        while (true) {
            if (tickets > 0) {
                System.out.println(Thread.currentThread().getName()+"卖出的第" + tickets-- + "张票");
            }
        }
    }

测试1,一切看似正常
程序代码:
package com.xiaoa.test;

import com.xiaoa.thread.Mythread;

public class ThreadTest {
    public static void main(String[] args) {
        Mythread tt = new Mythread();
        Thread t1 = new Thread(tt);//構造的線程必須傳入相同的runable,才可以共享成員變量
        Thread t2 = new Thread(tt);
        t1.setName("售票窗口1");
        t2.setName("售票窗口2");
        t1.start();
        t2.start();
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册

小睡一会的线程
程序代码:
package com.xiaoa.thread;

public class Mythread2 implements Runnable {
    int tickets = 100;//線程共享的成員變量

    @Override
    public void run() {
        while (true) {
            try {
                Thread.currentThread().sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (tickets > 0) {
                System.out.println(Thread.currentThread().getName()+"卖出的第" + tickets-- + "张票");
            }
        }
    }

}
测试2,发现了线程不安全的诡异问题
程序代码:
package com.xiaoa.test;

import com.xiaoa.thread.Mythread2;

public class ThreadTest2 {
    public static void main(String[] args) {
        Mythread2 tt = new Mythread2();
        Thread t1 = new Thread(tt);//構造的線程必須傳入相同的runable,才可以共享成員變量
        Thread t2 = new Thread(tt);
        t1.setName("售票窗口1");
        t2.setName("售票窗口2");
        t1.start();
        t2.start();
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册




[此贴子已经被作者于2018-8-23 14:58编辑过]

搜索更多相关主题的帖子: com thread public void new 
2018-08-23 14:56
new_bigbug
Rank: 2
等 级:论坛游民
威 望:1
帖 子:12
专家分:27
注 册:2018-9-22
收藏
得分:0 
会不会是没有将run()方法里的内容原子化呢?
程序代码:
public void run() {
        while (true) {
            synchronized(this)//加入这句应该可以解决吧
            {
                try {
                    Thread.currentThread().sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (tickets > 0) {
                    System.out.println(Thread.currentThread().getName()+"卖出的第" + tickets-- + "张票");
                }
            }
        }
    }
2018-09-23 11:04
melody87
Rank: 2
等 级:新手上路
威 望:3
帖 子:9
专家分:0
注 册:2015-6-13
收藏
得分:0 
同步,楼上正解
2019-02-27 15:17
GrayJerry
Rank: 5Rank: 5
等 级:贵宾
威 望:14
帖 子:75
专家分:310
注 册:2015-10-20
收藏
得分:0 
二楼正解,多个线程在访问同一份资源时,需要加:同步
2019-09-26 19:55
快速回复:关于多线程的demo
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017464 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved