/** * Java線程:線程的同步 * * @author leizhimin 2009-11-4 11:23:32 */ public class Test { public static void main(String[] args) { User u = new User("張三", 100); MyThread t1 = new MyThread("線程A", u, 20); MyThread t2 = new MyThread("線程B", u, -60); MyThread t3 = new MyThread("線程C", u, -80); MyThread t4 = new MyThread("線程D", u, -30); MyThread t5 = new MyThread("線程E", u, 32); MyThread t6 = new MyThread("線程F", u, 21); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); t6.start(); } } class MyThread extends Thread { private User u; private int y = 0; MyThread(String name, User u, int y) { super(name); this.u = u; this.y = y; } public void run() { u.oper(y); } } class User { private String code; private int cash; User(String code, int cash) { this.code = code; this.cash = cash; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } /** * 業務方法 * @param x 添加x萬元 */ public synchronized void oper(int x) { try { Thread.sleep(10L); this.cash += x; System.out.println(Thread.currentThread().getName() + "運行結束,增長「" + x + "」,當前用戶帳戶餘額爲:" + cash); Thread.sleep(10L); } catch (InterruptedException e) { e.printStackTrace(); } } @Override public String toString() { return "User{" + "code='" + code + '\'' + ", cash=" + cash + '}'; } }
本文出自 「熔 巖」 博客,請務必保留此出處http://lavasoft.blog.51cto.com/62575/221914java