线程共享资源的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://xxlcube.blog.csdn.net/article/details/17613725



package demo;


public class Demo implements Runnable {

	private int apple = 5;
	
	synchronized boolean  getApple() {
		if(apple>0) {
			apple--;
			System.out.println(Thread.currentThread().getName()+"拿走了一个苹果!还剩"+apple+"个苹果!");
			return true;
		}
		return false;
	}
	
	@Override
	public void run() {
		boolean flag = getApple();
		while(flag) {
			flag = getApple();
		}
		System.out.println(Thread.currentThread().getName()+"拿完了苹果!");
	}
	
	public static void main(String[] args) {
		Demo d = new Demo();
		
		Thread one = new Thread(d);
		Thread two = new Thread(d);
		one.setName("Simon");
		two.setName("Bella");
		one.start();
		two.start();
	}
	
}

达到线程安全的共享apple这个变量,关键在于关键字synchronized,读者可以自行删除该关键字,查看运行结果。

展开阅读全文

线程共享资源问题

11-12

大家好,小弟第一次发帖,如果有什么不合适的地方,还请大家多多包涵。rn[code=Java]rnpublic class ThreadTest rnrn /**rn * @param argsrn */rn public static void main(String[] args) rnrn ThreadDemo td = new ThreadDemo();rn Thread t = new Thread(td, "thread_test");rn Thread t2 = new Thread(td, "thread_test2");rn t.start();rn t2.start();rnrn rnrnrnclass ThreadDemo implements Runnable rn private int i=0;rn public void run() rn for (; i < 100; i++) rn System.out.println(Thread.currentThread().getName()+":"+i);rn rn rnrn[/code]rn我对以上的代码理解是:应该输出100行,两个线程共享一个资源,直到i为100rn可是输出如下:rnthread_test:0rnthread_test:1rnthread_test:2rnthread_test:3rnthread_test:4rnthread_test:5rnthread_test:6rnthread_test:7rnthread_test:8rnthread_test:9rnthread_test:10rnthread_test:11rnthread_test:12rnthread_test:13rnthread_test:14rnthread_test:15rnthread_test:16rnthread_test:17rnthread_test:18rnthread_test:19rnthread_test:20rnthread_test:21rnthread_test:22rnthread_test:23rnthread_test:24rnthread_test:25rnthread_test:26rnthread_test:27rnthread_test:28rnthread_test:29rnthread_test:30rnthread_test:31rnthread_test:32rnthread_test:33rnthread_test:34rnthread_test:35rnthread_test:36rnthread_test:37rnthread_test:38rnthread_test:39rnthread_test:40rnthread_test:41rnthread_test:42rnthread_test:43rnthread_test:44rnthread_test:45rnthread_test:46rnthread_test:47rnthread_test:48rnthread_test:49rnthread_test:50rnthread_test:51rnthread_test:52rnthread_test:53rnthread_test:54rnthread_test:55rnthread_test:56rnthread_test:57rnthread_test:58rnthread_test:59rnthread_test:60rnthread_test:61rnthread_test:62rnthread_test:63rnthread_test:64rnthread_test:65rnthread_test:66rnthread_test:67rnthread_test:68rnthread_test:69rnthread_test:70rnthread_test:71rnthread_test:72rnthread_test:73rnthread_test:74rnthread_test:75rnthread_test:76rnthread_test:77rnthread_test:78rnthread_test:79rnthread_test:80rnthread_test:81rnthread_test:82rnthread_test:83rnthread_test:84rnthread_test:85rnthread_test:86rnthread_test:87rnthread_test:88rnthread_test:89rnthread_test:90rnthread_test:91rnthread_test:92rnthread_test:93rnthread_test:94rnthread_test:95rnthread_test:96rnthread_test:97rnthread_test:98rnthread_test:99rn[color=#FF0000]thread_test2:32[/color]rn我想请问一下大家,最后一个数据 thread_test2:32是哪里来的,如果是两个线程共享的话,顶多交替进行,可是32输出了两遍,是在是不理解。请大家帮我看看。我是线程的初学者,请大家解释的清楚点,谢谢哦。rn 论坛

没有更多推荐了,返回首页