diff --git a/java/java 多线程与并发/Synchronized原理分析/加锁与释放锁流程.jpg b/java/java 多线程与并发/Synchronized原理分析/加锁与释放锁流程.jpg new file mode 100644 index 0000000..047fee8 Binary files /dev/null and b/java/java 多线程与并发/Synchronized原理分析/加锁与释放锁流程.jpg differ diff --git a/java/java 多线程与并发/synchronized 关键字/对象锁:实例1.txt b/java/java 多线程与并发/synchronized 关键字/对象锁:实例1.txt deleted file mode 100644 index 709727e..0000000 --- a/java/java 多线程与并发/synchronized 关键字/对象锁:实例1.txt +++ /dev/null @@ -1,28 +0,0 @@ - -class A implements Runnable { - static A a = new A(); // ´´½¨Àà±äÁ¿ - @Override - public void run() { - - // this±ØÐëÊÇͬһ¸öËø¶ÔÏ󣨶à¸öÏ̱߳ØÐëÓÃÏàͬµÄËø¶ÔÏó£© - synchronized (this) { - System.out.println("ÎÒÊÇÏ߳Ì" + Thread.currentThread().getName()); - try { - Thread.sleep(3000); - } catch (Exception e) { - - } - System.out.println(Thread.currentThread().getName() + "½áÊø"); - } - } -} -class B { - public static void main(String[] args) { - Thread thread1 = new Thread(A.a); - Thread thread2 = new Thread(A.a); - Thread thread3 = new Thread(A.a); - thread1.start(); - thread2.start(); - thread3.start(); - } -} \ No newline at end of file diff --git a/java/java 多线程与并发/synchronized 关键字/方法锁:实例1.txt b/java/java 多线程与并发/synchronized 关键字/方法锁:实例1.txt deleted file mode 100644 index 09630a8..0000000 --- a/java/java 多线程与并发/synchronized 关键字/方法锁:实例1.txt +++ /dev/null @@ -1,27 +0,0 @@ -class A implements Runnable { - static A a = new A(); // ´´½¨Àà±äÁ¿ - - @Override - public void run() { - this.method(); - } - - public synchronized void method() { - System.out.println("ÎÒÊÇÏ߳Ì" + Thread.currentThread().getName()); - try { - Thread.sleep(3000); - } catch (Exception e) { - e.printStackTrace(); - } - System.out.println(Thread.currentThread().getName() + "½áÊø"); - } -} - -class B { - public static void main(String[] args) { - Thread thread1 = new Thread(A.a); - Thread thread2 = new Thread(A.a); - thread1.start(); - thread2.start(); - } -} \ No newline at end of file diff --git a/java/java 多线程与并发/多线程与并发.xmind b/java/java 多线程与并发/多线程与并发.xmind index 4fad1a4..a74d702 100644 Binary files a/java/java 多线程与并发/多线程与并发.xmind and b/java/java 多线程与并发/多线程与并发.xmind differ diff --git a/java/studyTest/.idea/.gitignore b/java/studyTest/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/java/studyTest/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/java/studyTest/.idea/inspectionProfiles/Project_Default.xml b/java/studyTest/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..6560a98 --- /dev/null +++ b/java/studyTest/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,36 @@ + + + + \ No newline at end of file diff --git a/java/studyTest/.idea/misc.xml b/java/studyTest/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/java/studyTest/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/java/studyTest/.idea/modules.xml b/java/studyTest/.idea/modules.xml new file mode 100644 index 0000000..636e501 --- /dev/null +++ b/java/studyTest/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/java/studyTest/.idea/uiDesigner.xml b/java/studyTest/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/java/studyTest/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/java/studyTest/.idea/vcs.xml b/java/studyTest/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/java/studyTest/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/java/studyTest/javap b/java/studyTest/javap new file mode 100644 index 0000000..e69de29 diff --git a/java/studyTest/src/thread/SynchronizedTest_1.java b/java/studyTest/src/thread/SynchronizedTest_1.java new file mode 100644 index 0000000..bbc9fa3 --- /dev/null +++ b/java/studyTest/src/thread/SynchronizedTest_1.java @@ -0,0 +1,33 @@ +package thread; + +/** + * synchronized用在静态方法上:相当于是类锁 + */ +public class SynchronizedTest_1 implements Runnable{ + + static SynchronizedTest_1 instence1 = new SynchronizedTest_1(); // 锁1 + static SynchronizedTest_1 instence2 = new SynchronizedTest_1(); // 锁2 + + @Override + public void run() { + method(); + } + + // synchronized用在静态方法上,默认的锁就是当前所在的Class类,所以无论是哪个线程访问它,需要的锁都只有一把 + public static synchronized void method() { + System.out.println("我是线程" + Thread.currentThread().getName()); + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println(Thread.currentThread().getName() + "结束"); + } + + public static void main(String[] args) { + Thread t1 = new Thread(instence1); + Thread t2 = new Thread(instence2); + t1.start(); + t2.start(); + } +} diff --git a/java/studyTest/src/thread/SynchronizedTest_2.java b/java/studyTest/src/thread/SynchronizedTest_2.java new file mode 100644 index 0000000..07ee396 --- /dev/null +++ b/java/studyTest/src/thread/SynchronizedTest_2.java @@ -0,0 +1,32 @@ +package thread; + +/** + * synchronized用在普通方法上:默认的是this + */ +public class SynchronizedTest_2 implements Runnable{ + static SynchronizedTest_2 test = new SynchronizedTest_2(); // 创建类变量 + + @Override + public void run() { + method(); + } + + public synchronized void method() { + System.out.println("我是线程" + Thread.currentThread().getName()); + try { + Thread.sleep(3000); + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println(Thread.currentThread().getName() + "结束"); + } + + public static void main(String[] args) { + Thread thread1 = new Thread(SynchronizedTest_2.test); + Thread thread2 = new Thread(SynchronizedTest_2.test); +// Thread thread3 = new Thread(new SynchronizedTest_2()); // 此时无法实现线程同步 + thread1.start(); + thread2.start(); +// thread3.start(); + } +} diff --git a/java/studyTest/studyTest.iml b/java/studyTest/studyTest.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/java/studyTest/studyTest.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file