jars="" for j in `ls $libs/*.jar` do jars=$jars:$j done
Use the above jars varible in the classpath.
jars="" for j in `ls $libs/*.jar` do jars=$jars:$j done
public class SingletonSample { private static SingletonSample instance; private SingletonSample(){} public static SingletonSample getInstance() { if (instance == null) // 1 {instance = new SingletonSample();} // 2 return instance; // 3 } }In the above design, the constructor is declared as private and the getInstance() method ensures that only a single object is created. This program will work well for single threaded program. But however, for multithreaded programs this pattern will fail inconsistently. Let’s have a look at how and where this pattern will fail.
synchronized public static SingletonSample getInstance() { if (instance == null) // 1 {instance = new SingletonSample();} // 2 return instance; // 3 }The above code works fine for multithreaded access to getInstance() method but on deeper analysis we find that the ‘synchronization’ is actually required only for the first invocation as only on the first invocation will the line // 2 be actually executed, which also happens to be the line which needs to be synchronized. All other invocation apart from the first will get a not-null value and return the reference. However, it also adds up to performance issue as the complete getInstance() method is synchronized and that any other invocation will have to wait for the lock to be released to execute the getInstance().
public static SingletonSample getInstance() { if (instance == null) // 1 synchronized(SingletonSample.class){instance = new SingletonSample();} // 2 return instance; // 3 }However, in the above code too, we face the same problem as we had in code 1.Two threads can enter the IF block after finding instance is null. Now, Thread 1 enters the ‘synchronized’ block and creates the Object. Thread 2 also enters the synchronized block after Thread 1 releases the lock and also creates one more object. Now, in this case the Thread 2 does not check for the instance==null and directly creates a second Object.
public static SingletonSample getInstance() { if (instance == null) synchronized(SingletonSample.class){ // 1 if(instance==null) // 2 instance = new SingletonSample(); // 3 } return instance; }Thus by implementing the second check on instance variable makes it impossible for creation of more than one SingletonSample objects.
public static Singleton getInstance() { return instance; }
import java.util.*; import java.io.*; public class BigFile implements Iterable<String> { private BufferedReader _reader; public BigFile(String filePath) throws Exception { _reader = new BufferedReader(new FileReader(filePath)); } public void Close() { try { _reader.close(); } catch (Exception ex) {} } public Iterator<String> iterator() { return new FileIterator(); } private class FileIterator implements Iterator<String> { private String _currentLine; public boolean hasNext() { try { _currentLine = _reader.readLine(); } catch (Exception ex) { _currentLine = null; ex.printStackTrace(); } return _currentLine != null; } public String next() { return _currentLine; } public void remove() { } } }
BigFile file = new BigFile("C:\\Temp\\BigFile.txt"); for (String line : file) System.out.println(line);
public static String getFile(String filepath) { StringBuilder output = new StringBuilder(""); try { File file = new File(filepath); FileReader fileReader = new FileReader(file); BufferedReader bfr = new BufferedReader(fileReader); String line ; while((line = bfr.readLine()) != null) { output.append(line + "\n"); } bfr.close(); fileReader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { } return output.toString(); }
@Action public void createCode() { String txt = txtarea.getText(); String[] s2=txt.split("\\n"); StringBuilder sb = new StringBuilder(""); sb.append("public static String createCode()\n"); sb.append("{\n"); sb.append("\tStringBuilder sb=new StringBuilder(\"\");\n"); for(String s:s2) { sb.append("\tsb.append(\""); sb.append(s); sb.append("\\n\");\n"); } sb.append("\treturn sb.toString();"); sb.append("\n}"); code.setText(sb.toString()); }