一、内置类与静态内置类
关键字涉及到的内置类的使用
(一)、普通内置类
package com.it.po.thread06; public class PublicClass { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } //内置类 class PrivateClass{ private Integer age; private String address; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public void printlnPublicProperty(){ System.out.println("username= "+username+" password= "+password); } } ; }
package com.it.po.thread06; public class RunClass1 { public static void main(String[] args){ PublicClass publicClass = new PublicClass(); publicClass.setPassword("123"); publicClass.setUsername("po"); System.out.println("publicClass 用户:"+publicClass.getUsername()+
" 密码 "+publicClass.getPassword()); PublicClass.PrivateClass privateClass = publicClass.new PrivateClass(); privateClass.setAge(18); privateClass.setAddress("河池"); System.out.println("privateClass 地址:"+privateClass.getAddress()+
" 年龄 "+privateClass.getAge()); } }
publicClass 用户:po 密码 123
privateClass 地址:河池 年龄 18
注意:如果 RunClass1 和 PublicClass 不在一个包中, 需要将内置类声明为public
实例化内置类必须:
PublicClass.PrivateClass privateClass = publicClass.new PrivateClass();
(二)、静态内置类
package com.it.po.thread06; public class PublicClass { private static String username; private static String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } //内置类 static class PrivateClass{ private Integer age; private String address; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public void printlnPublicProperty(){ System.out.println("username= "+username+" password= "+password); } } ; }
package com.it.po.thread06; public class RunClass1 { public static void main(String[] args){ PublicClass publicClass = new PublicClass(); publicClass.setPassword("324"); publicClass.setUsername("lan"); System.out.println("publicClass 用户:"+publicClass.getUsername()+
" 密码 "+publicClass.getPassword());
PublicClass.PrivateClass privateClass = new PublicClass.PrivateClass(); privateClass.setAge(19); privateClass.setAddress("深圳"); System.out.println("privateClass 地址:"+privateClass.getAddress()+
" 年龄 "+privateClass.getAge()); } }
publicClass 用户:lan 密码 324
privateClass 地址:深圳 年龄 19
(三)、静态内置类同步方法
package com.it.po.thread06; public class OutClass { static class Inner{ public void methodA(){ synchronized ("其他锁"){ for(int i=1;i<=10;i++){ try { System.out.println("线程 "+Thread.currentThread().getName()+" i= "+i); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); }; } } } //内置类方法2 synchronized public void methodB(){ for(int i=1;i<=10;i++){ try { System.out.println("线程 "+Thread.currentThread().getName()+" i= "+i); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); }; } } } }
package com.it.po.thread06; public class OutClassRun1 { public static void main(String[] args){ OutClass.Inner inner = new OutClass.Inner(); Thread t1 = new Thread(new Runnable() { @Override public void run() { inner.methodA(); } },"A"); Thread t2 = new Thread(new Runnable() { @Override public void run() { inner.methodB(); } },"B"); t1.start(); t2.start(); } }
线程 B i= 1 线程 A i= 1 线程 B i= 2 线程 A i= 2 线程 B i= 3 线程 A i= 3 线程 B i= 4 线程 A i= 4 线程 B i= 5 线程 A i= 5 线程 B i= 6 线程 A i= 6 线程 B i= 7 线程 A i= 7 线程 B i= 8 线程 A i= 8 线程 B i= 9 线程 A i= 9 线程 B i= 10 线程 A i= 10
(四)、静态内置类同步方法2
package com.it.po.thread06; public class OutClass2 { static class InnerClass1{ public void methodA(InnerClass2 innerClass2){ String threadName=Thread.currentThread().getName(); synchronized (innerClass2){//锁第二个内置类 for(int i=1;i<=10;i++){ try { System.out.println("线程 "+threadName+" InnerClass1 methodA i= "+i); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); }; } } } //内置类方法2 synchronized public void methodB(){ String threadName=Thread.currentThread().getName(); for(int i=1;i<=10;i++){ try { System.out.println("线程 "+threadName+" InnerClass1 methodB i= "+i); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); }; } } } //内置类2 static class InnerClass2{ synchronized public void methodA(){ String threadName=Thread.currentThread().getName(); for(int i=1;i<=10;i++){ try { System.out.println("线程 "+threadName+"InnerClass2 i= "+i); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); }; } } } }
package com.it.po.thread06; public class OutClassRun2 { public static void main(String[] args){ OutClass2.InnerClass1 innerClass1 = new OutClass2.InnerClass1(); OutClass2.InnerClass2 innerClass2 = new OutClass2.InnerClass2(); Thread t1 = new Thread(new Runnable() { @Override public void run() { innerClass1.methodA(innerClass2); } },"A"); Thread t2 = new Thread(new Runnable() { @Override public void run() { innerClass1.methodB(); } },"B"); Thread t3 = new Thread(new Runnable() { @Override public void run() { innerClass2.methodA(); } },"C"); t1.start(); t2.start(); t3.start(); } }
线程 A InnerClass1 methodA i= 1 线程 B InnerClass1 methodB i= 1 线程 A InnerClass1 methodA i= 2 线程 B InnerClass1 methodB i= 2 线程 A InnerClass1 methodA i= 3 线程 B InnerClass1 methodB i= 3 线程 A InnerClass1 methodA i= 4 线程 B InnerClass1 methodB i= 4 线程 A InnerClass1 methodA i= 5 线程 B InnerClass1 methodB i= 5 线程 A InnerClass1 methodA i= 6 线程 B InnerClass1 methodB i= 6 线程 A InnerClass1 methodA i= 7 线程 B InnerClass1 methodB i= 7 线程 A InnerClass1 methodA i= 8 线程 B InnerClass1 methodB i= 8 线程 A InnerClass1 methodA i= 9 线程 B InnerClass1 methodB i= 9 线程 A InnerClass1 methodA i= 10 线程 B InnerClass1 methodB i= 10 线程 CInnerClass2 i= 1 线程 CInnerClass2 i= 2 线程 CInnerClass2 i= 3 线程 CInnerClass2 i= 4 线程 CInnerClass2 i= 5 线程 CInnerClass2 i= 6 线程 CInnerClass2 i= 7 线程 CInnerClass2 i= 8 线程 CInnerClass2 i= 9 线程 CInnerClass2 i= 10
线程AB异步,线程AC是同步的。
二、锁对象的改变
(一)、字符变更
package com.it.po.thread07; public class MyService1 { private String lock="123"; public void methodA(){ synchronized (lock) { try { String threadName = Thread.currentThread().getName(); System.out.println("进入时间time= "+System.currentTimeMillis()); System.out.println("线程: " + threadName + " 进入方法methodA"); lock="234"; Thread.sleep(2000); System.out.println("线程: " + threadName + " 离开方法methodA"); System.out.println("出去时间time= "+System.currentTimeMillis()); } catch (InterruptedException e) { e.printStackTrace(); } } } }
package com.it.po.thread07; import com.it.po.thread03.ThreadA; public class Run1 { public static void main(String[] args){ try { MyService1 my = new MyService1(); Thread t1 = new Thread(new Runnable() { @Override public void run() { my.methodA(); } },"A"); //线程2 Thread t2 = new Thread(new Runnable() { @Override public void run() { my.methodA(); } },"B"); t1.start(); //Thread.sleep(100);//让线程t1 和t2 获取到不同的锁 t2.start(); } catch (Exception e) { e.printStackTrace(); } } }
进入时间time= 1573967149090 线程: A 进入方法methodA 线程: A 离开方法methodA 出去时间time= 1573967151092 进入时间time= 1573967151092 线程: B 进入方法methodA 线程: B 离开方法methodA 出去时间time= 1573967153092
同步效果
package com.it.po.thread07; import com.it.po.thread03.ThreadA; public class Run1 { public static void main(String[] args){ try { MyService1 my = new MyService1(); Thread t1 = new Thread(new Runnable() { @Override public void run() { my.methodA(); } },"A"); //线程2 Thread t2 = new Thread(new Runnable() { @Override public void run() { my.methodA(); } },"B"); t1.start(); Thread.sleep(100);//让线程t1 和t2 获取到不同的锁 t2.start(); } catch (Exception e) { e.printStackTrace(); } } }
进入时间time= 1573967255348 线程: A 进入方法methodA 进入时间time= 1573967255448 线程: B 进入方法methodA 线程: A 离开方法methodA 出去时间time= 1573967257349 线程: B 离开方法methodA 出去时间time= 1573967257448
异步的效果
(二)、对象属性变更
package com.it.po.thread07; public class MyService2 { public void methodA(UserInfo userInfo){ synchronized (userInfo){ try { String threadName = Thread.currentThread().getName(); System.out.println("进入时间time= "+System.currentTimeMillis()); System.out.println("线程: " + threadName + " 进入方法methodA"); userInfo.setPassword("poo"); Thread.sleep(2000); System.out.println("线程: " + threadName + " 离开方法methodA"); System.out.println("出去时间time= "+System.currentTimeMillis()); } catch (InterruptedException e) { e.printStackTrace(); } } } }
package com.it.po.thread07; public class MyThread2_1 extends Thread{ private MyService2 myService2; private UserInfo userInfo; public MyThread2_1(MyService2 myService2, UserInfo userInfo) { this.myService2 = myService2; this.userInfo = userInfo; } @Override public void run() { super.run(); myService2.methodA(userInfo); } }
package com.it.po.thread07; public class MyThread2_2 extends Thread{ private MyService2 myService2; private UserInfo userInfo; public MyThread2_2(MyService2 myService2, UserInfo userInfo) { this.myService2 = myService2; this.userInfo = userInfo; } @Override public void run() { super.run(); myService2.methodA(userInfo); } }
package com.it.po.thread07; public class Run2 { public static void main(String[] args){ try { MyService2 my = new MyService2(); UserInfo userInfo = new UserInfo(); MyThread2_1 my1 = new MyThread2_1(my,userInfo); MyThread2_2 my2 = new MyThread2_2(my,userInfo); my1.setName("A"); my2.setName("B"); my1.start(); Thread.sleep(1000); my2.start(); } catch (Exception e) { e.printStackTrace(); } } }
进入时间time= 1573968127310 线程: A 进入方法methodA 线程: A 离开方法methodA 出去时间time= 1573968129311 进入时间time= 1573968129311 线程: B 进入方法methodA 线程: B 离开方法methodA 出去时间time= 1573968131311
对象 UserInfo 属性虽然改变了,但是对象本身没有变,还是同步的。
浙公网安备 33010602011771号