JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label Java-Multithreading. Show all posts
Showing posts with label Java-Multithreading. Show all posts

Multithreading in Java

9:54 PM
Multithreading in Java Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process. Threads can be created by using two mechanisms : 1. Extending the Thread class 2. Implementing the Runnable Interface Thread creation by extending the Thread class We create a class that extends the java.lang.Thread class. This class overrides the run() method available in the Thread class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the Thread object. // Java code for thread creation by extending  // the Thread class  class MultithreadingDemo extends Thread  {      public void run()      {          try         {              // Displaying the thread that is running       

Java - Multithreading

9:58 PM
Java is a  multi-threaded programming language  which means we can develop multi-threaded program using Java. A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs. By definition, multitasking is when multiple processes share common processing resources such as a CPU. Multi-threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application. Multi-threading enables you to write in a way where multiple activities can proceed concurrently in the same program. Life Cycle of a Thread A thread goes through various stages in its life cycle. For example, a thread i

.