The Artima Developer Community
Sponsored Link

Java Buzz Forum
Create Thread without extending Thread and implementing Runnable

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Create Thread without extending Thread and implementing Runnable Posted: Feb 10, 2015 10:18 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Create Thread without extending Thread and implementing Runnable
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

Using AnonymousThread:

  1. package com.instanceofjava;
  2.  
  3. public class AnonymousThread
  4. {
  5.  
  6. public static void main(String[] args)
  7.     {
  8.         new Thread(){
  9.  
  10.             public void run(){
  11.  
  12.                 for (int i = 0; i  <=10; i++) {
  13.                    System.out.println("run"+i);
  14.                 }
  15.  
  16.             }
  17.  
  18.         }.start();
  19.  
  20.         for (int i = 0; i  <=10; i++) {
  21.             System.out.println("main:"+i);
  22.         }
  23. }
  24. }

Output:

  1. main:0
  2. run:0
  3. main:1
  4. run:1
  5. main:2
  6. run:2
  7. main:3
  8. run:3
  9. main:4
  10. run:4
  11. main:5
  12. run:5
  13. main:6
  14. run:6
  15. main:7
  16. run:7
  17. main:8
  18. run:8
  19. main:9
  20. run:9
  21. main:10
  22. run:10

 

Using AnonymousRunnable:

  1. package com.instanceofjava;
  2.  
  3. public class AnonymousRunnable
  4. {
  5.  
  6. public static void main(String[] args)
  7.     {
  8.         new Thread(new Runnable(){
  9.  
  10.  
  11.     public void run(){
  12.  
  13.                 for (int i = 0; i  <=10; i++) {
  14.                    System.out.println("run"+i);
  15.                 }
  16.  
  17.             }
  18.  }
  19. ).start();
  20.  
  21.         for (int i = 0; i  <=10; i++) {
  22.             System.out.println("main:"+i);
  23.         }
  24. }
  25. }

Output:

  1. main:0
  2. run0
  3. main:1
  4. run1
  5. main:2
  6. run2
  7. main:3
  8. run3
  9. main:4
  10. run4
  11. main:5
  12. run5
  13. main:6
  14. run6
  15. main:7
  16. run7
  17. main:8
  18. run8
  19. main:9
  20. run9
  21. main:10
  22. run10

Read: Create Thread without extending Thread and implementing Runnable

Topic: How JVMTI tagging can affect GC pauses Previous Topic   Next Topic Topic: Codename One Charts

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use