标签:
public class TestCode { public static void main(String[] args) { TestCode1 t1 = new TestCode1(); TestCode2 t2 = new TestCode2(); TestCode3 t3 = new TestCode3(); Thread ta = new Thread(t1, "A"); Thread tb = new Thread(t2, "B"); Thread tc = new Thread(t3, "C"); ta.start(); tb.start(); tc.start(); } private static TestCode test = new TestCode(); public static class TestCode1 implements Runnable { public synchronized void run() { for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+" synchronized loop "+i); } } } public static class TestCode2 implements Runnable { public void run() { synchronized (test) { //这里如果使用this则表示新实例,就不与其他代码块搞在一起 for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+" synchronized loop "+i); } } } } public static class TestCode3 implements Runnable { public void run() { synchronized (test) { //这里如果使用this则表示新实例,就不与其他代码块搞在一起 for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+" synchronized loop "+i); } } } } }
B synchronized loop 0 A synchronized loop 0 A synchronized loop 1 B synchronized loop 1 B synchronized loop 2 A synchronized loop 2 B synchronized loop 3 A synchronized loop 3 A synchronized loop 4 B synchronized loop 4 C synchronized loop 0 C synchronized loop 1 C synchronized loop 2 C synchronized loop 3 C synchronized loop 4
public class TestSynchronized { private int count = 0; public synchronized void plus() { System.out.println("begin plus count="+count); count++; try { Thread.sleep(2000); System.out.println("end plus count="+count); } catch (Exception e) { e.printStackTrace(); } } public synchronized void minus() { System.out.println("begin minus count="+count); count--; System.out.println("end minus count="+count); } public void print() { try { Thread.sleep(1000); System.out.println("print count="+count); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { final TestSynchronized ts = new TestSynchronized(); Thread thread_plus = new Thread("thread_plus") { public void run() { System.out.println("Thread:" + super.getName()); ts.plus(); } }; final Thread thread_minus = new Thread("thread_minus") { public void run() { System.out.println("Thread:" + super.getName()); //改变ts.sub和ts.print的执行顺序,看看是什么情况 ts.minus(); ts.print(); // ts.minus(); } }; thread_plus.start(); Thread.sleep(1000); thread_minus.start(); } }
Thread:thread_plus begin plus count=0 Thread:thread_minus end plus count=1 begin minus count=1 end minus count=0 print count=0
import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.locks.ReentrantLock; public class TestAnonymous { public static String getNowDateTime() { SimpleDateFormat s_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d_date = new Date(); String s_date = ""; s_date = s_format.format(d_date); return s_date; } private static void runSync() { for (int i = 0; i < 5; i++) { final int pos = i; Thread t = new Thread() { @Override public synchronized void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(getNowDateTime() + " runSync pos=" + pos); } }; t.start(); } } private final static ReentrantLock lock = new ReentrantLock(); private static void runLock() { for (int i = 0; i < 5; i++) { final int pos = i; Thread t = new Thread() { @Override public void run() { try { lock.lock(); Thread.sleep(1000); System.out.println(getNowDateTime() + " runLock pos=" + pos); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } }; t.start(); } } public static void main(String[] args) { runSync(); runLock(); } }
2016-04-20 11:25:36 runSync pos=1 2016-04-20 11:25:36 runSync pos=4 2016-04-20 11:25:36 runSync pos=2 2016-04-20 11:25:36 runSync pos=3 2016-04-20 11:25:36 runSync pos=0 2016-04-20 11:25:36 runLock pos=0 2016-04-20 11:25:37 runLock pos=1 2016-04-20 11:25:38 runLock pos=2 2016-04-20 11:25:39 runLock pos=3 2016-04-20 11:25:40 runLock pos=4
标签:
原文地址:http://blog.csdn.net/aqi00/article/details/51200409