标签:多线程
<pre name="code" class="java">package cn.thread; public class Boo { private int x = 100; public int getX() { return x; } public synchronized int fix(int y) { x = x - y; System.out.println(Thread.currentThread().getName() + " : 当前boo对象的y值= " + x); return x; } // //同步代码块 // public int fix(int y) { // synchronized (this) { // x = x - y; // System.out.println(Thread.currentThread().getName() + " : 当前boo对象的y值= " + x); // } // // return x; // } }
package cn.thread; public class MyRunnable implements Runnable { private Boo foo = new Boo(); public static void main(String[] args) { MyRunnable run = new MyRunnable(); Thread ta = new Thread(run, "Thread-A"); Thread tb = new Thread(run, "Thread-B"); ta.start(); tb.start(); } public void run() { for (int i = 0; i < 3; i++) { this.fix(30); try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } public int fix(int y) { return foo.fix(y); } }注意的问题:
标签:多线程
原文地址:http://blog.csdn.net/u013777676/article/details/45971387