Results 81 to 100 of 116
- 09-14-2008, 03:48 PM #81
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 578
- Rep Power
- 6
If the the lock object is created in class B
private Object lock = new Object(); // object for common use
and the get method b.getLockObject() returns lock of class B in class A
what am I actually waiting on???
b.getLockObject().wait();
I am very much puzzled that the wait and notifying works in following context (getLockObject().notify() is at class B) there is no relation with the thread p-start() object:
synchronized (getLockObject()) { //use the common lock object
getLockObject().notify();
}
Java Code:public void run() { // run method of class Play System.out.println("entering in thread " + threadcount); synchronized (b.getLockObject()){ try{ b.getLockObject().wait(); } catch(InterruptedException e){ System.out.println(e); } } soundblock(); // this.notify(); System.out.println("exit thread " + threadcount); }
- 09-14-2008, 06:25 PM #82
?.....
this.notifiy is probably not workable code. The wait is accomplished in b.getLockObject().wait - but that is not how you wait - we're getting two issues up on the board here. A lock, as coded, is a synchronization method - which is accomplished by making getLock() synchronized. Further, it has to be:
You do not notifiy this, notifiy only talks to other - which is not a keyword I have seen except from the Hexagon at Redmond.Java Code:public synchronized getLock(boolean release){}
Further, you do not get (lock) from waiting thread - you wait() as in second.wait(); which will do all the convoluted work ad hoc. Second is made a variable in First by doing Second second = new Second(); in class First. The this keyword does all the fancy stuff of determining which object is being referenced in a forest of references.Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 09-14-2008, 08:42 PM #83
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 578
- Rep Power
- 6
problem solved?
I think the dummy is finally working.
There is just one do loop
but it goes to sleep and does not spin
The previous idea of class A B
and backwards notifying stands
But next time should be different
Java Code:package waitthread; /** * * @author willemjav */ public class A { private B b; private playSoundblock p; private appLoop loop; private int threadcount=0; private Object looplock = new Object(); // object A() { // constructor creates an object of the GUI class B b = new B(); Loop(); // starts the app } private void play() { // the method that creates the thread object p = new playSoundblock(); p.start(); } private void Loop() { // the method that creates the thread object loop = new appLoop(); loop.start(); } private class appLoop extends Thread { // the thread that plays the soundblocks appLoop() { } public void run() { System.out.println("entering in wait thread "); while (true) { // condition can be set by window listener play(); // starts the play thread b.wakePlaythread(); // the start-button of the soundblock synchronized (looplock){ // while in play thread waitloop sleeps try{ looplock.wait(); } catch(InterruptedException e){ System.out.println(e); } } } // System.out.println("exit wait thread " + threadcount2); } } // end of wait thread class private class playSoundblock extends Thread { // the thread that plays the soundblocks playSoundblock() { threadcount++; } public void run() { System.out.println("entering in play thread " + threadcount); synchronized (b.getPlaylockObject()){ // play thread waits for start try{ // to start the soundblock b.getPlaylockObject().wait(); } catch(InterruptedException e){ System.out.println(e); } } soundblock(); synchronized (looplock) { // when exits playthread wakes up waitloop looplock.notify(); } System.out.println("exit play thread " + threadcount); } } // end of play thread class private void soundblock() { System.out.println("soundblock started"); } // here is all sound midi data public static void main(String[] args) { new A(); } } //and of first public class
Java Code:package waitthread; import java.io.IOException; import javax.swing.JPanel; public class B extends JPanel { // all the GUI stuff of the app. Boolean flg; private Object playlock = new Object(); // object for common use public B() { System.out.println("constructor of B loaded"); } public Object getPlaylockObject(){ // share the app lock return playlock; } public Boolean getflg() { // starts the soundblock return flg; } public void wakePlaythread() { try { do { // simulating start button int d = System.in.read(); } while(System.in.available()>0); } catch (IOException ex) { ex.printStackTrace(); } synchronized (getPlaylockObject()) { // wakes up play thread getPlaylockObject().notify(); } } } // end of second public class
- 09-14-2008, 08:59 PM #84
Hey Norm! Look at this!.... wow
Wow, constructor starts nested class as a Thread object, then exits with no reference to the thread,... okay ~ okay already, places ref in outer class varaible == good OO probably(!) Class B - which is the Gui thread,......
Gotta quit, I'm gonna bust out the glass in my monitor from laughing.
( seriously - will be back in a while ....)Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 09-15-2008, 08:25 AM #85
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 578
- Rep Power
- 6
The dummy works....... pull it through the compiler and try it out (use the monitor keyboard to get out of the loop)
The application will be closed by setting the do (true) false of the A class. The flag will be set by the B class through a window listener. So the classical
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
wont be used here.
Can anybody give me a hint how a window listener works when clossing the app.?
- 09-15-2008, 10:04 AM #86
Member
- Join Date
- Sep 2008
- Posts
- 1
- Rep Power
- 0
New Commer
Hi, hello members. I wish all the best for ypur success.
- 09-15-2008, 10:17 AM #87
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 578
- Rep Power
- 6
thanks, anything else you like to say?
- 09-15-2008, 04:50 PM #88
we move on
unecessarily hard.Why?Discounting proven methods introduces unforseen failure modes.How to Write Window Listeners
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 09-16-2008, 01:48 PM #89
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 578
- Rep Power
- 6
I did read that stuff
"How to Write Window Listeners"
But what you do, for instances, when you also want to catch the quit from the menu bar.
Just before quitting the app I need to do some clean up (e.g. disconnecting the midi drivers). Besides the main thread there are two more and I do not know which one is running when the users quits (at any time).... so where to locate the final clause and how I know that that will be called only just before closing the app.
Remember I never did any CS (Computer School)!
- 09-16-2008, 02:20 PM #90
Is that when the user clicks on the close button (X) in the upper right hand corner of the window?catch the quit from the menu bar.
A window listener can be used to catch (and ignore if you want) the clicking on that button.
- 09-16-2008, 04:26 PM #91
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 578
- Rep Power
- 6
when running a java app there are basically two ways to quit
1) the (x) on pc
2) the only menu drop down with the app name (on mac) and choose the quit option
How to catch both options
- 09-16-2008, 04:40 PM #92
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 578
- Rep Power
- 6
quiting a java app.
read this
General: Exit Java applications properly
Define the following action class:
public class Quit extends javax.swing.AbstractAction {
public void actionPerformed(java.awt.event.ActionEvent event) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
System.exit(0);
}
});
}
}
Then attach an instance of this action to the "Quit" button or menu item
just like you would any other action.
If you have a "main" application window, and assuming that it is an instance
of JFrame, you can also try the following:
frame.setDefaultCloseOperation(javax.swing.JFrame. EXIT_ON_CLOSE);
Then, when the frame is closed, the application will exit.
Hope this helps,
Ramesh Gupta
eNode — Powerful XML Infrastructure
- 09-16-2008, 04:41 PM #93
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 578
- Rep Power
- 6
To install your own quite button
still one has to disable the (x) and the quite option
how?
- 09-16-2008, 06:30 PM #94
Is the (x) what we see in the upper right of a window?disable the (x) and the quite option
how?
What is the quit(e) option? Is that on Mac vs windows?
Since some of us are on Windows vs mac, we can't see what you see.
Try writing a very small program with a GUI that has the features that you seem with your GUI. Then run thru the different ways of closing/ending the application, and post them here with your code.
Trapping windowClosing in a listener is one way to handle clicking on the X in the upper right.
- 09-16-2008, 07:15 PM #95
Computer School!
This has been discussed in other fora, who arrived at 'cannot be done', which sounds incorrect on the face of it. There should be a WINDOW_EVENT WindowClosing whereby one can do cleanup - a standard hook in a reasonable operating system: Java is too sophisticated not to have one, but we are likely to risk thread cross-lock issues if we go about in frame of observation willemjav is using to code the last posted to which I made discouting observations until it became a safety risk.
willemjav: until you grasp the fundamental nature of threading, you are likely to cross-lock on program exit.
================= <- hardware
||||||||||||||||||||||
||||||||||||||||||||||
||||||||||||||||||||||
numerous threads
if two or greater try to lock on two objects, there must be effective design by systems coders, application coder techniques will generally introduce unforseen failure modes. Maybe we have a mathemetician in the observation pool who can explain this to willemjav.
Envision two post grads, home for vacation, trying to generate Chapter Eleven filing fees by mowing lawns. There are two yards, each with two Gates of Hell. Each is trying to get the other to take a gate but to convey those instructions to the competitor, each must obtain one gate in one yard. Promptly, one grabs the Chrome Gate in Beauford's yard and the other grabs the Golden Gate in the Beatrice's yard, going into a wait state for the competitor to release their FASI approved, tamper-resistant seal.
They can download all the software they want, but it will not release until one of them does a yield(). ( which is correctly coded as sleep(0) )
When will this ever end?Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 09-16-2008, 08:13 PM #96
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 578
- Rep Power
- 6
what is a cross-lock?
On mac I can quit a java app in two ways!
the window (x) (on left side on mac)
and the top menu bar shows the name of the jave app (amung others like apple drop down etc). The menu-drop-down of the java app has a quit option too, so there are two to catch on mac!!!
- 09-16-2008, 08:17 PM #97
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 578
- Rep Power
- 6
I do not even now how to get to the quit-option-one
(the window one sort-of)
- 09-17-2008, 12:50 AM #98
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 578
- Rep Power
- 6
What about this, nick?
I do not know how, but it works, a secure clean upJava Code:Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { System.out.println("calling from hook"); cleanUp(); }; }); private void cleanUp() { cont.ed.resetFbo(); cont.ed.closeFbo1(); System.out.println(" exit and clean up "); }
- 09-17-2008, 12:51 AM #99
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 578
- Rep Power
- 6
create the thread in the constructor of Main
- 09-17-2008, 01:06 AM #100
Dramatic improvement, cross-lock is terminology I came up with. No one is paying the internet to do development, so we get some archaics in contemporary practice. Your last code looks extremely promising, we can discount the two avenues if you use the shutdown hook you posted.
CrossLock is usually called deadlock because of what happens if it happens in the wrong places.Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Similar Threads
-
passing a value from parent thread to child thread
By sachinj13 in forum Threads and SynchronizationReplies: 7Last Post: 09-07-2008, 09:06 PM -
Waiting for a button to be pressed
By SomeGuyOverThere in forum New To JavaReplies: 6Last Post: 08-21-2008, 09:30 PM -
waiting for a file
By Fleur in forum New To JavaReplies: 2Last Post: 06-23-2008, 08:18 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM


LinkBack URL
About LinkBacks


Bookmarks