• We just launched and are currently in beta. Join us as we build and grow the community.

Creating Threads in Java

nahuel002

Cyber Awareness Educator
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
59
Likes
68
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
Today in Java, I'm going to teach you how to create a program that will create threads. We all know that a thread is a basic processing unit in our computer to which an operating system allocates processor time, and more than one thread can be executing code inside a process. The Java Virtual Machine (JVM) allows an application to have multiple threads of execution running concurrently. Thus, threads have priority. It is just like a racing program.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of thread.java.

2. Import java.io package. Hence we will use an input/output in accessing files, with IOException.
  1. import

    java.io.IOException

    ;

3. Provide a Thread extension in your classname and declare the following variables below.
  1. class

    thread extends

    Thread

    {
  2. private

    String

    carName;
  3. private

    long

    aWhile;

4. Create a constructor named thread that is the same with the class name with the parameters of String carName, long delay. Make the delay variable equal to the aWhile variable that we have created above. Make the setDaemon equals to True. A daemon thread is also a thread, that does not prevent the Java Virtual Machine(JVM) from exiting when the program finishes but the thread is still running.
  1. public

    thread(

    String

    carName, long

    delay)

    {
  2. this

    .carName

    =

    carName;
  3. aWhile =

    delay;
  4. setDaemon(

    true

    )

    ;
  5. }

5. Create a method named run for running the thread when using the start keyword. This will display the carnames and have a delay in displaying it.

  1. public

    void

    run(

    )

    {
  2. try

    {
  3. while

    (

    true

    )

    {
  4. System

    .out

    .print

    (

    carName)

    ;
  5. sleep(

    aWhile)

    ;

  6. }
  7. }

    catch

    (

    InterruptedException

    e)

    {
  8. System

    .out

    .println

    (

    carName +

    e)

    ;
  9. }
  10. }

6. In your Main, create three threads that have the carnames and the delay parameters.

  1. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  2. Thread

    first =

    new

    thread(

    "Toyota \n

    "

    , 100L)

    ;
  3. Thread

    second =

    new

    thread(

    "Honda \n

    "

    , 200L)

    ;
  4. Thread

    third =

    new

    thread(

    "Suzuki \n

    "

    , 500L)

    ;
  5. System

    .out

    .println

    (

    "Press Enter when you have had enough...\n

    "

    )

    ;

To start running threads, put this code below.

  1. first.start

    (

    )

    ;
  2. second.start

    (

    )

    ;
  3. third.start

    (

    )

    ;

To stop our thread from running, put this Try and Catch statement below. The thread will stop if we press Enter.

  1. try

    {
  2. System

    .in

    .read

    (

    )

    ;
  3. System

    .out

    .println

    (

    "Enter pressed...\n

    "

    )

    ;
  4. }

    catch

    (

    IOException

    e)

    {
  5. System

    .out

    .println

    (

    e)

    ;
  6. }


Full source code:

  1. import

    java.io.IOException

    ;

  2. class

    thread extends

    Thread

    {

  3. private

    String

    carName;
  4. private

    long

    aWhile;


  5. public

    thread(

    String

    carName, long

    delay)

    {
  6. this

    .carName

    =

    carName;
  7. aWhile =

    delay;
  8. setDaemon(

    true

    )

    ;
  9. }


  10. public

    void

    run(

    )

    {
  11. try

    {
  12. while

    (

    true

    )

    {
  13. System

    .out

    .print

    (

    carName)

    ;
  14. sleep(

    aWhile)

    ;

  15. }
  16. }

    catch

    (

    InterruptedException

    e)

    {
  17. System

    .out

    .println

    (

    carName +

    e)

    ;
  18. }
  19. }



  20. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  21. Thread

    first =

    new

    thread(

    "Toyota \n

    "

    , 100L)

    ;
  22. Thread

    second =

    new

    thread(

    "Honda \n

    "

    , 200L)

    ;
  23. Thread

    third =

    new

    thread(

    "Suzuki \n

    "

    , 500L)

    ;
  24. System

    .out

    .println

    (

    "Press Enter when you have had enough...\n

    "

    )

    ;
  25. first.start

    (

    )

    ;
  26. second.start

    (

    )

    ;
  27. third.start

    (

    )

    ;
  28. try

    {
  29. System

    .in

    .read

    (

    )

    ;
  30. System

    .out

    .println

    (

    "Enter pressed...\n

    "

    )

    ;
  31. }

    catch

    (

    IOException

    e)

    {
  32. System

    .out

    .println

    (

    e)

    ;
  33. }

  34. }

  35. }


Output:

thread.png


Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,053

322,753

322,762

Top