Categories
discuss

Programmatically determine which Java thread holds a lock

Is it possible at runtime to programmatically check the name of the Thread that is holding the lock of a given object?

Answer

You can only tell whether the current thread holds a normal lock (Thread.holdsLock(Object)). You can’t get a reference to the thread that has the lock without native code.

However, if you’re doing anything complicated with threading, you probably want to familiarize yourself with the java.util.concurrent packages. The ReentrantLock does allow you to get its owner (but its a protected method, so you’d have to extend this). Depending on your application, it may well be that by using the concurrency packages, you’ll find that you don’t need to get the lock’s owner after all.

There are non-programmatic methods to find the lock owners, such as signaling the JVM to issue a thread dump to stderr, that are useful to determine the cause of deadlocks.

Categories
discuss

How do I format a number in Java?

How do I format a number in Java?
What are the “Best Practices”?

Will I need to round a number before I format it?

32.302342342342343 => 32.30

.7323 => 0.73

etc.

Answer

From this thread, there are different ways to do this:

double r = 5.1234;
System.out.println(r); // r is 5.1234

int decimalPlaces = 2;
BigDecimal bd = new BigDecimal(r);

// setScale is immutable
bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
r = bd.doubleValue();

System.out.println(r); // r is 5.12

f = (float) (Math.round(n*100.0f)/100.0f);

DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
double dd = 100.2397;
double dd2dec = new Double(df2.format(dd)).doubleValue();

// The value of dd2dec will be 100.24

The DecimalFormat() seems to be the most dynamic way to do it, and it is also very easy to understand when reading others code.

Categories
discuss

Preventing accidental double clicking on a button

I have a few controls that inherit from ASP.NET buttons and use onserverclick.

If the user clicks twice, the button fires two server side events. How can I prevent this?

I tried setting this.disabled='true' after the click (in the onclick attribute) via javascript, but that blocks the first postback as well.

Answer

See this example for disabling control on postback. It should help you do what you’re trying to achieve.

http://encosia.com/2007/04/17/disable-a-button-control-during-postback/

Categories
discuss

How to implement file upload progress bar on web?

I would like display something more meaningful that animated gif while users upload file to my web application. What possibilities do I have?

Edit: I am using .Net but I don’t mind if somebody shows me platform agnostic version.

Answer

Here are a couple of versions of what you’re looking for for some common JavaScript toolkits.

Categories
discuss

GreaseMonkey script to auto login using HTTP authentication

I’ve got quite a few GreaseMonkey scripts that I wrote at my work which automatically log me into the internal sites we have here. I’ve managed to write a script for nearly each one of these sites except for our time sheet application, which uses HTTP authentication.

Is there a way I can use GreaseMonkey to log me into this site automatically?

Edit: I am aware of the store password functionality in browsers, but my scripts go a step further by checking if I’m logged into the site when it loads (by traversing HTML) and then submitting a post to the login page. This removes the step of having to load up the site, entering the login page, entering my credentials, then hitting submit

Answer

It is possible to log in using HTTP authentication by setting the “Authorization” HTTP header, with the value of this header set to the string “basic username:password”, but with the “username:password” portion of the string Base 64 encoded.

http://frontier.userland.com/stories/storyReader$2159

A bit of researching found that GreaseMonkey has a a function built into it where you can send GET / POST requests to the server called GM_xmlhttpRequest

http://diveintogreasemonkey.org/api/gm_xmlhttprequest.html

So putting it all together (and also getting this JavaScript code to convert strings into base64 I get the following

http://www.webtoolkit.info/javascript-base64.html

var loggedInText = document.getElementById('metanav').firstChild.firstChild.innerHTML;
if (loggedInText != "logged in as jklp") {
    var username = 'jklp';
    var password = 'jklpPass';
    var base64string = Base64.encode(username + ":" + password);

    GM_xmlhttpRequest({
        method: 'GET',
        url: 'http://foo.com/trac/login',
        headers: {
            'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey/0.3',
            'Accept': 'application/atom+xml,application/xml,text/xml',
            'Authorization':'Basic ' + base64string,
        }
    });
}

So when I now visit the site, it traverses the DOM and if I’m not logged in, it automagically logs me in.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..