Whay Multi Threading is used in JAVA?

Multi threading is used in java. Thread means process.A process is execute at a time but in java we can handle more processes at a time and take response to them.So, my question is how it works and how it handles the threads or process of a program and execute the actual program. Please discuss Multi Threading deeply by which its basic concept can be cleared.


23 Answers
1-20 of  23
23 Answers
  • The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread.

  • The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread. 2.

  • The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread.

  • In Java mutlithreading means multiple jobs run at same time or simultaneously.
    For each task one thread is assigned, if there are multiple tasks multiple threads are assigned, by using this threads all tasks are executed at same time.
    Single application is a combination of multiple threads
    By using multi threading, we can use all cores of cpu very efficiently.
    For example :
    1.In a word pad, typing a text uses one thread and word correction is checked by another thread.
    2 . in whats app multiple users request the server at same time for sending message to another user, then server assigns each user one thread. Then all user requests are executed at same time.
    3. In gaming also multi threading is used
    Multithreading is popular and mostly used by every application.

  • Multi Threading in java means It is a process of executing multiple threads simultaneously.
    Thread is basically a light weight process,a smallest unit of processing.Maulti-Threading and Multi-Processing boths are used to achieve  multitasking.
    But we use multi-threading than multi-processing because threads share a common memory area.They don't allocate separate memory area to saves memory, and context-switching between the threads takes less time than process.
    Java Multithreading is mostly used in games, animation etc.

    Advantage of Java Multi-threading
    1.Threads are independant and we can perform multiple operation at same time.
    2.We can perform many operation at same time so it saves time.
    3.Thread are independent so it doesn't affect other threads if exception occur in a single thread....

  • Symuntanieous execution of two or more part of program to maximise utilize tha cpu time

  • the main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilization of the cpu time.

  • Normally any program runs by a single process. Single process will take input and do some computations and then shows output. Our operating system comes with multiprogrammig system. In that multiple process runs in parallel fassion. But still one program having one process.
    Java comes with multi-threading, it means your process can be splitted into multiple sub-processes. like, one thread will take input from user, meanwhile other will do some independent computations and may be another thread will display some output.
    Let's take one example, suppose you have to build a program which have to functionalities, 1st functionality is to browse videos and view it online, 2nd functionality is to download videos. If you do it by using single process, then it will be 1 functionality at a time, means you cannot download video when you are browsing videos or you cannot browse videos while app is downloading some videos.
    In multi-threading system, one thread can download the videos in background mode, and meanwhile you can view or browse the other videos.
    Now the question is, which application you like to use if you are user? single process or multi-threaded?
    If your answer is multi-threaded application which downloads in background mode then that's the reason why java supports multi-threading.

  • According to me Multi threading is used in java to execute multiple threads simultaneously. The threads are basically a light weight sub-processes which can be said as a smallest unit of processing. It is basically used to achieve multitasking in java. Remember the threads are not processes but a part of them or sub-processes.

  • Better resource utilization.
    Simpler program design in some situations.
    More responsive programs.
    By Default java runs on 2 threads.
    1. Main Thread.
    2.Garbage Collector. 

  • Multi Threading means It is a process of executing multiple threads simultaneously.
    Thread is basically a light weight process,a smallest unit of processing.Maulti-Threading and Multi-Processing boths are used to achieve  multitasking.
    But we use multi-threading than multi-processing because threads share a common memory area.They don't allocate separate memory area to saves memory, and context-switching between the threads takes less time than process.
    Java Multithreading is mostly used in games, animation etc.

    Advantage of Java Multi-threading
    1.Threads are independant and we can perform multiple operation at same time.
    2.We can perform many operation at same time so it saves time.
    3.Thread are independent so it doesn't affect other threads if exception occur in a single thread.
     

  • As there are two ways to create create:
    1. Extending Thread class.
    2. Implementing runnable interface.

    Here i took exapmle of runnable interface. What i did here is that, 
    1. Created main thread and printed it by getting Id.
    2. Implemented Arrylist for worker.(Created 5 different thread using arraylist and assigned the time of 5 sec for each.
    3. I used try and catch here because when we will interrupt the thread it will show interrupted exception we need to handle that.

    import java.util.ArrayList;  
    import java.util.Date;  
    import java.util.List;  
      
    public class Worker implements Runnable   
    {  
      public boolean running = false;  // Condition shows that no thread is running.
        
      public Worker ()  
      {  
        Thread thread = new Thread(this);  //main thread created.
        thread.start();  
      }  
        
      public static void main (String[] args) throws InterruptedException  
      {  
        List<Worker> workers = new ArrayList<Worker>();  // Here list is created for workers.
          
        System.out.println("This is currently running on the main thread, " +  //printing the main thread 
            "the id is: " + Thread.currentThread().getId());  
      
        Date start = new Date();  
      
        // starting 5 workers  
        for (int i=0; i<5; i++)  
        {  
          workers.add(new Worker());   
        }  
          
        // We must force the main thread to wait for all the workers  
        //  to finish their work before we check to see how long it  
        //  took to complete  
        for (Worker worker : workers)  // this is new for each loop(small and not very lengthy)
        {  
          while (worker.running)  
          {  
            Thread.sleep(100);  
          }  
        }  
          
        Date end = new Date();  
          
        long difference = end.getTime() - start.getTime();  
          
        System.out.println ("This whole process took: " + difference/1000 + " seconds.");  
      }  
        
      @Override  
      public void run()   
      {  
        this.running = true;  
        System.out.println("This is currently running on a separate thread, " +  
            "the id is: " + Thread.currentThread().getId());  
          
        try   
        {  
          // this will pause this spawned thread for 5 seconds  
          //  (5000 is the number of milliseconds to pause)  
          // Also, the Thread.sleep() method throws an InterruptedException  
          //  so we must "handle" this possible exception, that's why I've  
          //  wrapped the sleep() method with a try/catch block  
          Thread.sleep(5000);  
        }   
        catch (InterruptedException e)   
        {  

  • For example you have a long task to do, and you don't want to lock the User Interface, then you can do the task on another thread while your User Interface still works well. That would be one scenario.And another one would be to calculate something long (long running task in background) , that can be done in many steps that can be done in parallel.

  • for multiple process execution

  • The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. Depends on what you mean by 'executing tasks'. Multithreading would surely be beneficial if the threads process mutually independent data in a concurrent fashion - it reduces requirements for locks and probabilty of deadlocks increases in a super-linear fashion with the number of locks

  • Thread means another execution path in a program. it is used to achieve multitasking in our system, if you have a single core processor it just works like process based multi tasking means it works on context switching technique but now a days every computer is provided with multiple processor cores, which means there can be situation where a separate thread can be allocate to a separate process on a separate processor which increases the utilization of multiple cores as well as in same time multiple tasks also can be performed....(hence multi tasking is the answer which increase processor efficiency )

  • The multithreading concepts are used in java to reduce the complexity of a program. By using multithreading we can perform  mulitasks in  single program. we can handle the threads using sleep(seconds)  that much time the thread stopped from execution. after compeletion of the time then the thread starts its execution . 

  • We use thread concept in java to reduce the compile time for example consider that your program consist of 1000 lines if u compile that program it requires a lot of time in order to reduce a time of compilation we use multi thread, using that we can split up the large programs into small programs so that each and every small programs are compiled at the same time hence we can save a time.

  • To achive multitasking and we use multi threading because threads shares common area of memory and threads are independent so they doesn't block to other thread while executing and we can perform many task at time and save our time 

  • generally there is no concept available in java we don't perform multiple tasks at the same time
    consider the following ex:
    we see a movie audio and video considered as multithreading if there is no multithreading then we see the video in one time and hear the audio in another time so.there is no linking b/w them.
     

loadAnswer
1-20 of  23
Core Java

Didn't get the answer.
Contact people of Talent-Core Java directly by clicking here