Share on Google+Share on Google+

Harish Batchu
Explain about threads:how to start program in threads?
2 Answer(s)      5 years and 4 months ago
Posted in : Java Beginners
import java.util.*;
class AlphabetPrint  extends Thread
{
    public void print()
    {
        for(int i=1;i<=26;i++)
        {
            System.out.println((char)i);
        }
}
         public void run()
    {
        print();
    }
}

class NumberPrinter  extends Thread
{
    public void print()
    {
        for(int i=65;i<90;i++)
        {
            System.out.println(i);
        }
    }   
    public void run()
    {
        print();
    }
}

class AlphaNumericPrint
{
    public static void main(String[] args)
    {
        AlphabetPrinter ap = new AlphabetPrinter();
        ap.start();
        NumberPrinter np = new NumberPrinter();
        np.start();
    }
}

Ads

View Answers

December 13, 2011 at 3:50 PM




December 13, 2011 at 4:33 PM


Thread is a path of execution of a program. It can be called as single unit of execution in a program. A program can have more than one thread. Every program has at least one thread. Threads are used to do multiple activities simultaneously in a process. For example:- If you want to type something and show date and time simultaneously you can use threads. MS Word works on the principle of threads so that you can modify the file and print it simultaneously. Threads are called light weight processes.
Every java program has atleast one thread the main thread. When a java program starts, jvm creates the main thread and calls the program's main method. Additional thread is created by extending the Thread class.
Threads have three stages in its life. Start, run, stop. Threads can be started using start method, the start method automatically calls run method to make it running. To end the life cycle of the thread stop method is called or thread shows an exception or error or run method comes to an end.

In the above program 2 thread classes are created AlphabetPrint and NumberPrinter. The objects of the two classes is created in class AlphaNumericPrint and the two threads are started. When both are started the run() in each thread is automatically called and the lines in the corresponding run()s are executed. In the run method of NumberPrinter, print() is called which displays numbers from 65 to 89 and in the run() of AlphabetPrint calls print() of AlphabetPrint and displays the equivalent characters of integers 1 to 25.

Here the two processes displaying alphabets and numbers are done simultaneously.










Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads