Question:
How can I make my application in java run only once?
cloud4288
2008-04-09 00:34:11 UTC
I tried to implement the singleton pattern but it still doesn't work.
What I want it to do is to run an application without any duplicate instance during runtime.

Is there a way of preventing the program from running two instances???

ex.
I've created a swing program that shows a jframe.
I've created a jar file for it.
I've run the jar file an it shows the jframe.
Without closing the previous jframe I try to run the jar file again but it needs to do nothing because there is already an instance of that program.
Four answers:
anonymous
2008-04-09 01:33:50 UTC
I would treat the single run execution as a static method. But first I have to show you the old way. It is only puzzling at first because we have to think like 2 class interacting:



class Account {

private int balance = 0;



synchronized void deposit( float amount) {

deposit += amount;

}





class Customer extends Thread {

Account account;



Customer(Account account){

account = account;

}

public void run() {

try{

account.deposit(105f);

}

catch(Exception e) {

e.priintStackTrace;

}

}

Then, in JFrame, when you make the GUI:

Customer c = new Customer( );



only ONE instance can run with that JFrame.



=============

Because of Swing. Swing is always "animating" the widgets. Ex. the modern thread, the only thing in JFrame main():



SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});



the creation method in JFrame, note we already have a core, central thread running:





private static void createAndShowGUI() {

// System.out.println("Created GUI on EDT? "+

// SwingUtilities.

//isEventDispatchThread());



Account account = new Account();



JFrame f = new JFrame("Swing Paint Demo");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.add(new Customer( account );

f.setSize(250,250);

f.setVisible(true);



}



which leads to a new, modern Customer class. Instead of run():



Runnable code = new Runnable() {

public void run() {

account.deposit(105f);

}

};

// look to the thread in main()

if(SwingUtilities.

isEventDispatchThread()) {

code.run();

} else {

SwingUtilities.

invokeLater(code);

}



================

thread is the only way I know to Synchronize, but the older code technique could lead to a lock, which is impossible to untangle with edits.



Maybe someone else knows a shorter and perhaps cleaner code implementation for running only one instance of your class. The keyword here is running, not static.
Badlass
2008-04-09 01:05:11 UTC
There seems to be two main techniques, the socket technique and the file lock technique.



The socket technique basically tries to connect to a specific socket. If it can't than it assumes that there is another instance of the program running.



The file lock technique basically tries to acquire a lock on a specific file. If it can't than it assumes that there is another instance of the program running.



The link below has sample code for both techniques and a more detailed explanation.
elkind
2016-10-18 07:51:23 UTC
when you consider which you're processing each factor in series i'm unsure a Hashtable is your ultimate decision. Hashtables are optimized for random get entry to via key. you assert your app is multithreaded, yet once you're sequentially processing the Hashtable it sounds like basically one thread is doing that, so there are no synchronization themes. i may be vulnerable to objective some different strategies for velocity: * use an ArrayList fairly of a Hashtable once you're basically doing sequential processing and by no skill could desire to get entry to via key. * attempt a HashMap fairly of a Hashtable in case you may desire to get entry to via key as properly. in spite of the actuality that this is lots the comparable in theory that's a greater moderen series than Hashtable and can furnish greater suited overall performance. * in case you do could desire to get entry to via key in some circumstances however the sequential processing is the main important factor you may desire to even attempt a pair of arrays, one for the main important and the different for the entire get entry to. you may desire to sequentially technique the 2d array very without postpone and you may desire to write a small approach that did a binary seek on the 1st while finding a key (see Arrays.binarySearch(a,ok)) to get the index to apply for the 2d array. Crude, yet speedy. i won't be able to promise any of those procedures would be swifter, yet they shouldn't take too long to objective out. you may desire to get a delightful ask your self.
anonymous
2008-04-09 06:30:42 UTC
you can post your requrement at http://expert.hyparoffice.com/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...