April 12, 20178 yr I legit don't understand wtf is the point of them. This is the example code: class Loader extends Thread { public void run() { System.out.println(“Hello”); } } class Program { public static void main(String[] args) { Loader load = new Loader(); //load.setPriority(10); load.start(); } } So what's the point of using Threads? Can somebody please explain the usefulness of this? Are threads good because you can optimize the program and spilt the work between two processors? Or maybe they're good because you can change priority and make certain calculations more important than others? Edit: wtf is the runnable interface? I know its another way of creating threads but whats the point? Please & Thank You Edited April 12, 20178 yr by obasan
April 12, 20178 yr So you posed questions and then answered them for yourself... Maybe you could hire me, do my job for me and still pay me.
April 12, 20178 yr Ready and study my friend. http://www.javaworld.com/article/2074217/java-concurrency/java-101--understanding-java-threads--part-1--introducing-threads-and-runnables.html https://www3.ntu.edu.sg/home/ehchua/programming/java/j5e_multithreading.html
April 12, 20178 yr Imagine if you have a webserver that only operates on 1 thread synchronously. It could only handle one request at a time. Lets say a user visits and another user visits 1 second after, it has to grab his data from the database, do whatever calculations etc, and resolve the first users request before being able to even recognize the second user has sent a request to the website. This is due to the only thread being blocked from the first request, so no other requests can be considered.
April 13, 20178 yr The most important thing to understand with threads is that they run concurrently. Imagine you have a program that has a GUI with a button in it. If the button is pressed and you'd execute some heavy calculation (or wait for something) in the same thread (the GUI is running on and being repainted in) ... the whole application would go unresponsive. By using another thread to do the calculation it will keep responsive. It's generally much better to work with Runnables instead of threads as it's much more flexible. Also look up ExecutorService to run them properly.
April 14, 20178 yr Author On 4/12/2017 at 3:17 PM, Final said: So you posed questions and then answered them for yourself... Maybe you could hire me, do my job for me and still pay me. lol'd. i might just do that ;)
Create an account or sign in to comment