Posted by: instance of java Posted date: 09:51 /
Using AnonymousThread:
- package com.instanceofjava;
-
- public class AnonymousThread
- {
-
- public static void main(String[] args)
- {
- new Thread(){
-
- public void run(){
-
- for (int i = 0; i <=10; i++) {
- System.out.println("run"+i);
- }
-
- }
-
- }.start();
-
- for (int i = 0; i <=10; i++) {
- System.out.println("main:"+i);
- }
- }
- }
Output:
- main:0
- run:0
- main:1
- run:1
- main:2
- run:2
- main:3
- run:3
- main:4
- run:4
- main:5
- run:5
- main:6
- run:6
- main:7
- run:7
- main:8
- run:8
- main:9
- run:9
- main:10
- run:10
Using AnonymousRunnable:
- package com.instanceofjava;
-
- public class AnonymousRunnable
- {
-
- public static void main(String[] args)
- {
- new Thread(new Runnable(){
-
-
- public void run(){
-
- for (int i = 0; i <=10; i++) {
- System.out.println("run"+i);
- }
-
- }
- }
- ).start();
-
- for (int i = 0; i <=10; i++) {
- System.out.println("main:"+i);
- }
- }
- }
Output:
- main:0
- run0
- main:1
- run1
- main:2
- run2
- main:3
- run3
- main:4
- run4
- main:5
- run5
- main:6
- run6
- main:7
- run7
- main:8
- run8
- main:9
- run9
- main:10
- run10
No comments:
Post a Comment