Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a thread from shell script  RSS feed

 
Roshnara Rose
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have got a shell script which calls a java program(BuildPage.java). I have created a thread and is starting from BuildPage.java. My intention was not to build pages concurrently as this script is calling from many places. So i have created a Thread (CreatePage.java) and started it from BuildPage.java.
In this, i'm creating a lock file in the synchronised method and removing it after build is over.
From webapplication, i'm calling the script to build the pages from a servlet. While building, the application hangs. I'm not much familiar with Threads.
Please help me to get rid of this problem. Also please suggest any different way of implementation for this.

Thanks in advance..




// ============================================


// ============================================

// ============================================



[NK: Please UseCodeTags]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code:



The variable isExecute is a local variable. Since, no one can change (and changes) this value your run() method will never return unless you explicitly break from within the while() loop.
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see several issues here:

1)

You assign isExecute = getLockFile(); and then execute the while loop. The isExecute variable will never be changed, so the loop will be executed either forever (if getLockFile() returned true) or never (if it returned false). I think that you wanted to check the isExecuted condition each time (correct me if I'm wrong), so the following would be more correct:


2)
In the getLockFile() method you call wait() if the lock file exists. The wait() method is called on this object (the CreatePublicPages object). The object should be notified by another thread, but the only place it is notified is the removeLockFile() method of the same object, which is executed sequentially by the same object. So the bug is that:
- a Runnable CreatePublicPages (let's call it cpp) is created and started in a new thread
- cpp.run() is executed
- cpp.getLockFile() is entered from cpp.run()
- if a lock file exists, cpp.wait() is called, causing cpp's thread to stop the execution and to wait for a notification
- no-one can notify cpp, because the only place cpp is notified is in cpp.removeLockFile(), but cpp can never get there because it is waiting for notification - deadlock

The solution would be to use a common object for all CreatePublicPages threads and to synchronize on it and call the wait/notifyAll methods of tat object. Or, even better, to use some kind of a high-level mutex, like Semaphore or Lock. For further info on synchronization, have a look at the Sun's concurrency tutorial
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!