标签:
public class MyThread extends Thread { private static int COUNT = 5; private int ticket = COUNT; private String name; public MyThread(String s){ name = s; } @Override public void run() { for(int i = 0; i < COUNT; i++){ if(ticket > 0){ System.out.println(name + "-->" + ticket--); } } }
MyThread thread1 = new MyThread("thread1"); MyThread thread2 = new MyThread("thread2"); thread1.start(); thread2.start();
public class MyRunnable implements Runnable { private static int COUNT = 5; private int ticket = COUNT; @Override public void run() { for(int i = 0; i < COUNT; i++){ if(ticket > 0){ System.out.println("ticket-->" + ticket--); } } } }
MyRunnable runnable = new MyRunnable(); new Thread(runnable).start(); new Thread(runnable).start();
public int compareTo(String anotherString) { int len1 = value.length; int len2 = anotherString.value.length; //重点是这里!!! int lim = Math.min(len1, len2); char v1[] = value; char v2[] = anotherString.value; //重点是这里!!! int k = 0; while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } k++; } return len1 - len2; }
public class BankCount { private int count = 0;//余额 public void addMoney(int money){//存钱 count += money; System.out.println(System.currentTimeMillis() + "存入:" + money); System.out.println("账户余额:" + count); } public void getMoney(int money){//取钱 if(count - money < 0){ System.out.println("余额不足"); System.out.println("账户余额:" + count); return; } count -= money; System.out.println(System.currentTimeMillis() + "取出:" + money); System.out.println("账户余额:" + count); } }
public class BankTest { public static void main(String[] args) { final BankCount bankCount = new BankCount(); new Thread(new Runnable() {//取钱线程 @Override public void run() { while(true){ bankCount.getMoney(200); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); new Thread(new Runnable() {//存钱线程 @Override public void run() { while(true){ bankCount.addMoney(200); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } }
public synchronized void addMoney(int money){//存钱 count += money; System.out.println(System.currentTimeMillis() + "存入:" + money); System.out.println("账户余额:" + count); } public synchronized void getMoney(int money){//取钱 if(count - money < 0){ System.out.println("余额不足"); System.out.println("账户余额:" + count); return; } count -= money; System.out.println(System.currentTimeMillis() + "取出:" + money); System.out.println("账户余额:" + count); }
public void addMoney(int money){//存钱 synchronized(this){ count += money; System.out.println(System.currentTimeMillis() + "存入:" + money); System.out.println("账户余额:" + count); } } public void getMoney(int money){//取钱 synchronized(this){ if(count - money < 0){ System.out.println("余额不足"); System.out.println("账户余额:" + count); return; } count -= money; System.out.println(System.currentTimeMillis() + "取出:" + money); System.out.println("账户余额:" + count); } }
public class BankCount { private Lock lock = new ReentrantLock();//获取可重入锁 private int count = 0;//余额 public void addMoney(int money){//存钱 lock.lock(); try { count += money; System.out.println(System.currentTimeMillis() + "存入:" + money); System.out.println("账户余额:" + count); }finally{ lock.unlock(); } } public void getMoney(int money){//取钱 lock.lock(); try { if(count - money < 0){ System.out.println("余额不足"); System.out.println("账户余额:" + count); return; } count -= money; System.out.println(System.currentTimeMillis() + "取出:" + money); System.out.println("账户余额:" + count); } finally{ lock.unlock(); } } }
public class BankCount { private volatile int count = 0;//余额 public void addMoney(int money){//存钱 count += money; System.out.println(System.currentTimeMillis() + "存入:" + money); System.out.println("账户余额:" + count); } public void getMoney(int money){//取钱 if(count - money < 0){ System.out.println("余额不足"); System.out.println("账户余额:" + count); return; } count -= money; System.out.println(System.currentTimeMillis() + "取出:" + money); System.out.println("账户余额:" + count); } }
public class BankCount { private static ThreadLocal<Integer> count = new ThreadLocal<Integer>(){ protected Integer initialValue() { return 0; }; };//余额 public void addMoney(int money){//存钱 count.set(count.get() + money); System.out.println(System.currentTimeMillis() + "存入:" + money); System.out.println("账户余额:" + count.get()); } public void getMoney(int money){//取钱 if(count.get() - money < 0){ System.out.println("余额不足"); System.out.println("账户余额:" + count.get()); return; } count.set(count.get() - money); System.out.println(System.currentTimeMillis() + "取出:" + money); System.out.println("账户余额:" + count.get()); } }
public class BankCount { public synchronized void addMoney(int money){//存钱 synchronized(this){ //同步代码块 int i = 5; while(i-- > 0){ System.out.println(Thread.currentThread().getName() + ">存入:" + money); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } } public synchronized void getMoney(int money){//取钱 int i = 5; while(i-- > 0){ System.out.println(Thread.currentThread().getName() + ">取钱:" + money); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public class BankTest { public static void main(String[] args) { final BankCount bankCount = new BankCount(); new Thread(new Runnable() {//取钱线程 @Override public void run() { bankCount.getMoney(200); } },"取钱线程").start(); new Thread(new Runnable() {//存钱线程 @Override public void run() { bankCount.addMoney(200); } },"存钱线程").start(); } }
public void addMoney(int money){//存钱 synchronized(BankCount.class){ int i = 5; while(i-- > 0){ System.out.println(Thread.currentThread().getName() + ">存入:" + money); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static synchronized void getMoney(int money){//取钱 int i = 5; while(i-- > 0){ System.out.println(Thread.currentThread().getName() + ">取钱:" + money); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } }
标签:
原文地址:http://blog.csdn.net/shakespeare001/article/details/51321498