final修飾的對象咋辦?

上代碼 ` private static final Logger logger = LoggerFactory.getLogger(ThreadTest.class);html

public void test_1(final AuthorizeContractBean bean){
    new Thread(new Runnable() {
        [@Override](https://my.oschina.net/u/1162528)
        public void run() {
            bean.setBorrowerId("1");
            logger.info(bean.toString());
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();

}
[@Test](https://my.oschina.net/azibug)
public void test_2(){
    for (int i = 0; i < 1000; i++) {
        AuthorizeContractBean b = new AuthorizeContractBean();
        if(i % 2 == 0){
            b.setPlatformNo("aaa");
            b.setFileUrl("aaa-url");
            test_1(b);
        } else {
            b.setPlatformNo("bbbb");
            b.setFileUrl("bbb-url");
            test_1(b);
        }
    }
}`

最終輸出結果app

17:43:43.077 [Thread-5] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='aaa-url', platformNo='aaa'}
17:43:43.075 [Thread-2] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='bbb-url', platformNo='bbbb'}
17:43:43.077 [Thread-4] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='bbb-url', platformNo='bbbb'}
17:43:43.077 [Thread-3] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='aaa-url', platformNo='aaa'}
17:43:43.075 [Thread-1] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='aaa-url', platformNo='aaa'}
17:43:43.077 [Thread-6] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='bbb-url', platformNo='bbbb'}
17:43:43.078 [Thread-7] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='aaa-url', platformNo='aaa'}
17:43:43.079 [Thread-8] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='bbb-url', platformNo='bbbb'}
17:43:43.079 [Thread-9] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='aaa-url', platformNo='aaa'}
17:43:43.079 [Thread-10] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='bbb-url', platformNo='bbbb'}
17:43:43.079 [Thread-11] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='aaa-url', platformNo='aaa'}
17:43:43.079 [Thread-12] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='bbb-url', platformNo='bbbb'}
17:43:43.079 [Thread-13] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='aaa-url', platformNo='aaa'}

說明bean對象的值是能夠被改變的。ide

可是bean不能被從新指向別的引用。 示例圖url

總結:final修飾的對象,引用不可被改變,但對象內的值能夠被改變。但普通變量只能賦值一次。 參考網址:輸入連接說明.net

相關文章
相關標籤/搜索