内置类和对象锁改变 笔记记录
?? 當內置類是私有的,若實例化該類與內置類 在一個包下可以實例化。若不在一個包下要實例化,需要將內置類改為public
package com.inner;
public class PublicClass {
?? ?private String username;
?? ?private String password;
?? ?
?? ?
?? ?public class PrivateClass{
??? private String age;
??? private String address;
?? ?
?? ?public String getAge() {
?? ??? ?return age;
?? ?}
?? ?public void setAge(String age) {
?? ??? ?this.age = age;
?? ?}
?? ?public String getAddress() {
?? ??? ?return address;
?? ?}
?? ?public void setAddress(String address) {
?? ??? ?this.address = address;
?? ?}
?? ?
?? ?
?? ??? ?
?? ?}
?? ?
?? ?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;
?? ?}
}
package com.test;
import com.inner.PublicClass;
import com.inner.PublicClass.PrivateClass;
public class Run {
?? ?public static void main(String[] args) {
?? ??? ?PublicClass publicClass=new PublicClass();
?? ??? ?publicClass.setPassword("123456");
?? ??? ?publicClass.setUsername("a");
?? ??? ?
?? ??? ?PrivateClass privateClass=publicClass.new PrivateClass();
?? ??? ?privateClass.setAge("124");
??????? privateClass.setAddress("武漢");?? ??? ?
?? ?}
}
當內置類為靜態內置類時 不需要在使用publicClass.new PrivateClass() 實例化PrivateClass? 因為靜態變量 靜態內置類都屬于類所有,不屬于對象所有。
對象鎖改變:當開始執行時所有線程爭奪同一個對象鎖,線程同步,若兩個線程由于所對象的改變,并不是爭奪的同一個鎖的時候線程異步。
package com.inner;
public class MyService {
?? ?private String lock="123";
?? ?public void testMethod(){
?????? synchronized(lock){
?? ??? ?try{
?? ??? ??? ?System.out.println(Thread.currentThread().getName()+"?? begin? "+System.currentTimeMillis());
?? ??? ??? ?lock="456";
?? ??? ??? ?Thread.sleep(2000);
?? ??? ??? ?System.out.println(Thread.currentThread().getName()+"?? end??? "+System.currentTimeMillis());
?? ??? ??? ?
?? ??? ?}catch(InterruptedException e){
?? ??? ??? ?
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?
?? ??? ?
?? ?}
?? ?}
}
package com.inner;
public class ThreadA extends Thread{
? private MyService myService;
? public ThreadA(MyService myService){
?? ?? this.myService=myService;
?? ? ?
? }
? public void run(){
?? ?? myService.testMethod();
?? ? ?
?? ? ?
? }
?? ?
?? ?
}
package com.inner;
public class ThreadB extends Thread{
? private MyService myService;
? public ThreadB(MyService myService){
?? ?? this.myService=myService;
?? ? ?
? }
? public void run(){
?? ?? myService.testMethod();
?? ? ?
?? ? ?
? }
?? ?
?? ?
}
package com.inner;
public class Run {
?? ?public static void main(String[] args) {
?? ??? ?MyService myService=new MyService();
?? ??? ?ThreadA? thread1=new ThreadA(myService);
?? ??? ?thread1.setName("a");
?? ??? ?ThreadB thread2=new ThreadB(myService);
?? ??? ?thread2.setName("b");
?? ??? ?thread1.start();
?? ?/*?? ?try{
?? ??? ??? ?Thread.sleep(100);
?? ??? ??? ?
?? ??? ?}catch(InterruptedException e){
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?
?? ??? ?}*/
?? ??? ?thread2.start();
?? ??? ?
?? ??? ?
?? ?}
}
如果不加延時,兩個線程幾乎同時運行,爭奪同一個對象,假若線程a拿到了該對象鎖,即使在運行過程中lock="456" lock 改變了,但是線程2是等著線程a釋放掉“123”的對象鎖,如果加延時,線程a先運行,當執行到lock="456"時,線程b其實是得到對象“456”的鎖。對象不一樣因此異步。
總結
以上是生活随笔為你收集整理的内置类和对象锁改变 笔记记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java synchronized 与
- 下一篇: mybatis 笔记记录