Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to understand threads  RSS feed

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I am Chaitanya, I am having a problem in my program. I wrote a program which creates a JFrame which has a button "Generate bill" in it. When the user clicks on the button the bill must be generated. Suppose a printer(ordinary dot matrix) takes 10 seconds to print. I dont have a printer with me. What I did is I generated a delay. The problem arises here. Until the delay is finished the window(JFrame) is not responding. I implemented both the GUI and Bill classes in multi threading.

Here is what I have written:



Can anyone tell me where the problem is. Thank you all in advance. Have a nice day.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
printBill is just a method its not in its own thread, your action calls printBill from the EDT the one and only event dispatch thread that runs the GUI when thats blocked you can do nothing else GUI wise. I suspect what you wanted was your action performed should have started a new thread whose run did a printBill, then the GUI would respond while your printBill ran.

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Chris. Can you tell me how to run printBill method in separate thread and make the GUI respond when the printing is going on.

Thank you in advance.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is very rough and I'll probably we arrested by the code style police :-) but see if this example helps you, I tried to copy your style and not fly off into the newer thread stuff ...

When you click printBill it will do 30 seconds of work in the background while your GUI is still alive and well ...


 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chris, it worked. Now I understood threads.
 
isha krishnan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though the prolem is solved at Your end chris,i have one reponse.As i can from your code that
Bill() is getting instantiated first rather than starting GUI thread first which may be cause of your window JFRAME in inactive state the delay in the start of execution.

t=new Thread(this,"GUI");
bill=new Bill();
t.start();

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@isha krishnan: Hi isha, can you please explain your post in detail. I dint understand what you were trying to tell.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I can see what he's saying but I don't think its correct.

I think part of the confusion is the "GUI" thread name as you actually kind of have two a normal thread you happen to call GUI and the "Event Dispatch Thread" aka EDT (which you don't have to create its done for you) and also known as the GUI thread. The first is just any old thread you happen to have called GUI the second is the one all GUI actions, redraws, button clicks happen.

Block the thread you have called "GUI" and the GUI still works .. block the EDT and it won't, you can test which thread your on with .

I think that's the point, the only other thing I could think off was if he thought new Bill () blocked GUI creation but it spins off another thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!