Lubos Programming Blog

Lubos Blog about Programming (mainly Java) and Computers

Archive for November, 2009

Upgrading to Windows 7

Posted by lubosp on November 15, 2009

I am part time system administrator for small health-care provider, so I know that Windows 7 are in my future with purchases of the new computers (it is harder and harder to get new computer with Windows XP).

I already tried to install Windows 7 on my 6 year old HP laptop, and the experience wasn’t quite positive, see my blog here. But most of the technical publications rates Windows 7 quite well, so I decided to upgrade my main work/development laptop, Thinkpad R61 to Windows 7 Pro. At the same time I purchased Asus Eee PC netbook with Windows 7 Starter Edition.

Installation

My Thinkpad R61 had Windows Vista Home edition (32 bit), so the OS did not use all of the 4Gb memory in the laptop. Because I couldn’t do Windows 7 update (update doesn’t work going from Vista Home to Windows 7 Pro), I decided to bite the bullet and do the clean install of Windows 7 Pro 64bit to make use of full 4Gb of memory. I performed full backup and ran Easy Transfer Wizard (to Windows 7). Then ran Windows 7 Upgrade Advisor and uninstalled and/or upgraded a few applications advisor recommended to uninstall or upgrade (mainly ThinkVantage, Thinkpad applications, see here).

Installation went smoothly and it took about an hour. I then ran Windows update a few times to get the latest and greatest. Everything was running well NVidia drivers and other drivers (camera, audio) loaded well, networking was working, I could easily connect to my MyWorldBook NAS, and to my HP network printer (this might be the first Windows OS where the networking is working without glitches ;-). I then run Windows Easy Transfer to restore my settings.

As always, while some applications are running out of the box like Firefox, Eclipse, Notepad++ and others, some Windows applications have nasty habit to depend on registry entries, .dll libraries in Windows\system32 etc, and they had to be reinstalled. This is one of the major pains of upgrading or re-installing Windows OS. IMHO Microsoft should offer better application model to application developers so they can build applications that are easy to migrate between Windows OS installation, and application developers should do better job writing applications that do not depend on resources that disappear during Windows OS install/re-install.
BTW, I am using 2 disk partitions, C: and D: drive, and I do install most of the applications on D: drive, which makes it easier to install/re-install Windows OS. Even with that, it took me about a day to get my laptop in shape.

Windows 7 install creates Windows.old folder with core of the old (in my case Windows Vista) OS, so you can use it to recover applications and data missed by transfer wizard. Biggest installation disappointment was that Start Menu\Programs disappeared and they were also missing in Windows.old folders, so I had to rebuild them from scratch. That in itself is not trivial because I couldn’t find a way to access Start Menu folder (Permission Denied) with Windows Explorer even with administrator privileges. Fortunately I found this article, and downloaded and used SurF Explorer.

I do not use Programs menu that much, I instead maintain and share between my computers custom toolbars (folders in C:\&D (for development), C:\&U for utilities etc) which show up as D >> toolbar and that popup applications (folder content) when clicked on >>  in the toolbar. I use these toolbars for easy access to applications. These custom toolbars saved me from some more work chasing down all the applications and creating their links.

Windows XP mode is not installed with Windows 7 by default, here is the Microsoft documentation how to install Windows XP mode.

Few Problems

  • Desktop icons suddenly disappeared after reboot, it took me few minutes to find Desktop popup menu View/Show desktop icons that got unchecked for some reason
  • Power management is biggest problem ad disappointment so far. At night I just close the lid of my laptop and then come in the morning and open it. In two of the three days using Windows 7 after opening the lid in the morning the display was dark, there was no laptop activity (even if OS was not shut down), and I couldn’t wake up the laptop no matter what I did. I have to do hard shutdown holding I/O button and restart Windows 7. This worked seamlessly in Windows Vista. I am trying to set all Power options to Never, but so far laptop wake up function is failing!
  • Creating file associations is worse than it was in Vista or XP, the UI doesn’t allow you to specify details of the command, and executing java .jar files requires -jar option to execute a .jar file (double-click). I had to search and edit registry to set .jar file association with -jar option. This was actually problem with me installing JDK 32bit instead of JDK 64bit. After I uninstalled 32bit and installed 64bit, everything was working well, and .jar associations were set properly.

Applications I had to re-install

  • VMWare player
  • Snagit
  • PDANet – used http://junefabrics.com/pdanet/x64.php to install 64b driver, and used old PDANet 1.2 for Blackberry
  • HAVA
  • FileZilla site manager, here is how to find and restore site manager items
  • Set environment variables, I forgot to save env. variables (why there is no Import/Export for env. variables on Windows?), so I had to recreate quite a few of them (JAVA_HOME, GROOVY_HOME etc.)
  • Cygwin
  • VNC
  • Blackberry Desktop
  • Logmein
  • Lastpass
  • VLC Player
  • Java

Conclusion

So far I do agree with experts, that Windows 7 is the best Windows OS so far, yes even better than Windows XP. After disabling UAC (which was much easier than in Vista), I am happy Windows 7 user (with exception of power management mentioned above). Also Windows 7 Starter on our Asus Eee PC is running well with no glitches (on that laptop power management works as it should, have it set to sleep on close lid, wake up on open).

 

Advertisements

Posted in java | Leave a Comment »

Stopping or interrupting Java hanging thread

Posted by lubosp on November 15, 2009

As most of Java developers know and experienced, some Java frameworks including core Java libraries do have nasty habit to run for very, very loooong time or hang indefinitely. Classic example is Java regex library which can cause stack overflow, or it can hang indefinitely.

In order to prevent the whole application from hanging and to allow to stop or interrupt the offending hanging thread, I am using timeout thread that checks if the watched thread finished in allotted time, and if it did not the timeout thread interrupts the watched, hanging thread.

Abruptly stopping or interrupting a thread from different thread is not well supported in Java especially when Thread.stop() method was deprecated, see How to stop a thread article.

But the problem is that Java offers nothing better to forcefully interrupt the offending hanging thread. Following is an example code of the TimeoutThread:

package com.lingoport.scanner.util;

import java.util.Date;

/** Stop a thread after a given timeout has elapsed
* <P>
* A simple timeout class.  You give it a thread to watch and a timeout
* in milliseconds.  After the timeout has elapsed, the thread is killed
* with a Thread.stop().  If the thread finishes successfully before then,
* you can cancel the timeout with a done() call; you can also re-use the
* timeout on the same thread with the reset() call.
* <P>
*
*/

public class TimeoutThread implements Runnable {

private final Thread targetThread;
private long millis;
private final Thread watcherThread;
private boolean loop;
private boolean enabled;
private static final boolean TIMEOUT_DISABLED = System.getProperty("disable.timeout") != null;

/**
* Constructor. Give it a thread to watch, and a timeout in milliseconds.
* After the timeout has elapsed, the thread gets killed. If you want
* to cancel the kill, just call done().
*
* @param targetThread
* @param millis
*/
public TimeoutThread(Thread targetThread, long millis) {
this.targetThread = targetThread;
this.millis = millis;
if (TIMEOUT_DISABLED) {
watcherThread = null;
enabled = false;
} else {
watcherThread = new Thread(this);
enabled = true;
watcherThread.start();
// Hack - pause a bit to let the watcher thread get started.
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}

/**
* Constructor, current thread.
*
* @param millis
*/
public TimeoutThread(long millis) {
this(Thread.currentThread(), millis);
}

/**
* Call this when the target thread has finished.
*/
public synchronized void done() {
loop = false;
enabled = false;
notify();
}

/**
* Call this to restart the wait from zero.
*/
public synchronized void reset() {
loop = true;
notify();
}

/**
* Call this to restart the wait from zero with a different timeout value.
*
* @param millis
*/
public synchronized void reset(long millis) {
this.millis = millis;
reset();
}

/*
* The watcher thread - from the Runnable interface.
* This has to be pretty anal to avoid monitor lockup, lost threads, etc.
*
*  (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public synchronized void run() {
if (TIMEOUT_DISABLED) return;
Thread me = Thread.currentThread();
me.setPriority(Thread.MAX_PRIORITY);
if (enabled) {
do {
loop = false;
try {
wait(millis);
} catch (InterruptedException e) {
}
} while (enabled && loop);
}
// The call stop() is deprecated, but Java doesn't offer anything better
if (enabled && targetThread.isAlive()) {
targetThread.stop();
done();
}
}

// Test main
public static void main(String[] args) {
System.out.println((new Date()) + "  Setting ten-second timeout...");
TimeoutThread tk = new TimeoutThread(10000);
try {
double f = 1.;
System.out.println((new Date()) + "  Starting execution of long loop...");
for(double i = 0; i < 1.0E99; i++) f = f * i;
System.out.println((new Date()) + "  Another execution of long loop...");
for(double i = 0; i < 1.0E99; i++) f = f * i;
tk.done();
} catch (Exception e) {
System.out.println((new Date()) + "  Caught Exception");
} catch (ThreadDeath td) {
System.out.println((new Date()) + "  Caught ThreadDeath");
}
System.out.println((new Date()) + "  Finished!");

}

}

This solution seems to be working quite well, and the offending hanging thread (caused for example by Java regex) can catch ThreadDeath exception, recover appropriately and continue work.

The only problem I encountered so far is that it doesn’t play well with Eclipse debugging, that is why I disable the TimoutThread while debugging the application using disable.timeout system property.

I know, that this is not great solution, but it works, and I couldn’t find any better solution (BTW, Thread.interrupt() does not work in this case). Let me know if you have better solution.

 

Posted in java | Leave a Comment »