- class Test
- {
- private Test()
- {
- System.out.println("private");
- }
- static Test ts = null;
- public static Test getTest()
- {
- return (null == ts ? ts = new Test() : null);
- }
- public void show()
- {
- System.out.println("hello private");
- }
- }
- class DemoDanli
- {
- public static void main(String[] args)
- {
- Test ts = Test.getTest();
- ts.show();
- }
- }
复制代码 请问第10行代码:return (null == ts ? ts = new Test() : null);这样写能否避免多线程访问产生的错误?是不是只要写一行代码就不会出错?
|