Question : Java multi thread singleton

I have following two codes, exisitng codes seems not thread safe. Am I right?


public class Test {

    public static Test instance =null;
    private Test()
    {
    }

     public synchronized static getInstance(){                  <--- add synchronized
        if(instance ==  null)
            instance = new Test();

         return instance;
     }

     business logic
}
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
/**********************Existing codes******************/

public class Test {

     public static Test instance =null;
     private Test()
     {
     }

     public static getInstance(){
        if(instance ==  null)
            instance = new Test();

         return instance;
     }

     business logic
}

Answer : Java multi thread singleton

try now:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
pathToZipFile="C:\temp\test.zip"
extractTo="C:\temp\unzip"

set fs = CreateObject("Scripting.FileSystemObject")
set objLog = fs.CreateTextFile("c:\temp\output.log")
	
Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd /c mkdir " & extractTo
Set oShell = Nothing

Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.getfolder(extractTo)
For each oFile in  folder.Files
	oFile.Delete(True)
Next

set sa = CreateObject("Shell.Application")
set ns = sa.NameSpace(pathToZipFile)
set filesInzip = ns.items
sa.NameSpace(extractTo).CopyHere(filesInzip) 

EnumCSV extractTo

objLog.Close

sub EnumCSV(targetdir)
	Dim fso
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set folder = fso.getfolder(targetdir)
	For Each File in Folder.Files
		If fso.GetExtensionName(File)="csv"Then
			ParseCSV(File)		
		End If
	Next
end sub

sub ParseCSV(csv_file)
	set fs = CreateObject("Scripting.FileSystemObject")
	set objTextFile = fs.OpenTextFile(csv_file)
	dim arrStr,lines,lastdate,lasttime
	lines=0
	
	Do while NOT objTextFile.AtEndOfStream
		arrStr = split(objTextFile.ReadLine,",")
		lines=lines+1
	Loop
	
	lastdate = Replace(arrStr(0),"""","")
	lasttime = Replace(arrStr(1),"""","")
	
	objLog.WriteLine "File: " & csv_file
	objLog.WriteLine "Total Lines: " & lines
	objLog.WriteLine "Last date: " & lastdate
	objLog.WriteLine "Last time: " & lasttime
	objLog.WriteLine
	objTextFile.Close
end sub
Random Solutions  
 
programming4us programming4us