Guest User

Untitled

a guest
Mar 17th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.09 KB | None | 0 0
  1. package com.myapp.patterns.creational;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class TestSingleton {
  8.    
  9.     private static TestSingleton instance=null;
  10.     private TestSingleton() {
  11.         // TODO Auto-generated constructor stub
  12.     }
  13.    
  14.     public static TestSingleton getInstance(){
  15.        
  16.         synchronized (TestSingleton.class) {
  17.            
  18.             if(instance == null){
  19.                 try {
  20.                     Thread.sleep(1000);
  21.                 } catch (InterruptedException e) {
  22.                    
  23.                     e.printStackTrace();
  24.                 }
  25.                 instance=new TestSingleton();
  26.             }
  27.             return instance;
  28.         }
  29.     }
  30.    
  31. private static final String url="jdbc:mysql://localhost:3306/citrix";
  32.    
  33.     private static final String username="admin";
  34.    
  35.     private static final String password="admin";
  36.    
  37.     private Connection connection=null;
  38.    
  39.    
  40.     public Connection getConnection() throws SQLException{
  41.        
  42.         try {
  43.             Thread.sleep(1000);
  44.         } catch (InterruptedException e) {
  45.             // TODO Auto-generated catch block
  46.             e.printStackTrace();
  47.         }
  48.        
  49.         if(connection==null || connection.isClosed()){
  50.            
  51.            
  52.     synchronized (TestSingleton.class) {
  53.                
  54.                 if(connection==null || connection.isClosed()){
  55.                 connection=DriverManager.getConnection(url, username, password);
  56.                 }
  57.             }
  58.         }
  59.         return connection;
  60.     }
  61.    
  62.     static TestSingleton instance1=null;
  63.     static TestSingleton instance2=null;
  64.     public static void main(String[] args) {
  65.        
  66.        
  67.        
  68.        
  69.     Runnable task1=()->{
  70.          instance1=TestSingleton.getInstance();
  71.        
  72.     };
  73.    
  74.     Runnable task2=()->{
  75.          instance2=TestSingleton.getInstance();
  76.        
  77.     };
  78.    
  79.    
  80.     Thread t1=new Thread(task1);
  81.     Thread t2=new Thread(task2);
  82.    
  83.     t1.start();
  84.     t2.start();
  85.    
  86.    
  87.         try {
  88.             t1.join();
  89.             t2.join();
  90.         } catch (InterruptedException e) {
  91.             // TODO Auto-generated catch block
  92.             e.printStackTrace();
  93.         }
  94.         System.out.println(instance1 == instance2);
  95.         try {
  96.             Connection con1=instance1.getConnection();
  97.             Connection con2=instance2.getConnection();
  98.             System.out.println(con1==con2);
  99.         } catch (SQLException e) {
  100.             // TODO Auto-generated catch block
  101.             e.printStackTrace();
  102.         }
  103.  
  104.     }
  105.  
  106. }
Add Comment
Please, Sign In to add comment