-5

I have a general question. I have been reading a Java book and I came across a program that uses Threads. Book stated that Threads are used for multiprocessing. I want to know that if I write :

Thread t=new Thread(new classname);
t.start;
//after it some GUI code to display the input received from user in run method

and I override run method to get input from user,then, will it wait for input and then perform GUI tasks like opening frame or it will perform both tasks simultaneously.

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • Suppose you have created a video software. It can be used for downloading videos as well as viewing and converting at the same time. To implement this in java, threads are required. – frunkad Feb 11 '15 at 19:47
  • use Google to search this question. – prime Feb 11 '15 at 19:47
  • 1
    The question in the title is very different from the question in the body of the post. Please clarify. – abl Feb 11 '15 at 19:49
  • 3
    This question is well out of scope of this site as it is far too broad. Beyond that, it asks one question title and then brings up a completely different question in the body. – Zéychin Feb 11 '15 at 19:49
  • Welcome to StackOverflow. To ask about what X is needed for, you should first get a basic understanding of what X is. Read about Java threads and then come back with a more specific question, if you will have one. – gvlasov Feb 11 '15 at 19:54
  • Generally speaking, GUIs are single threaded, that is, all the UI code should run within a single thread, blocking in anyway would have cause the UI to become unresponsive and ruin the user experience. Many UI Toolkits have means by which you can "stop" the vide execution at a given point (lookup modal dialogs) which are safe for user within the UI thread – MadProgrammer Feb 11 '15 at 20:23
  • You need Threads to separate the work of your program so if for example the user has to drop a folder into the program with 100000 files this is work for extra Thread or Threads so the main Thread of the Program not stack and the User during the background Proccessing can do other things and not wait 10 minutes for example unable to do something cause of Stack. – crAlexander Feb 11 '15 at 20:35

1 Answers1

1

They'll happen simultaneously. (Unless you block one of the threads using locks or a semaphore.)

If the gui thread relies on the input processing of the other thread, you'll have a race condition. So you'll definitely want to block the gui thread until the other thread is done producing whatever the gui thread needs.

As for why threads are needed, well, it's so tasks can be done simultaneously so programs can do their jobs faster.

Kacy
  • 3,330
  • 4
  • 29
  • 57
  • 1
    *"So you'll definitely want to block the gui thread until the other thread is done producing whatever the gui thread needs"* - No, not, nada. You should never actively block the UI, this makes your application unresponsive and can have adverse affects on your users experience. Better to use some kind of call back that the thread can use to tell the UI that it's finished farther the actively blocking the UI thread – MadProgrammer Feb 11 '15 at 20:20
  • @MadProgrammer True, I wasn't thinking in terms of UI, just the idea of 2 threads competing for resources. Thanks for pointing that out. – Kacy Feb 11 '15 at 21:17