Вопрос по java – создание потоков через внутренний класс

4

Error: User Rate Limit Exceeded

public class Mythread3 {
    public static void main(String... a) {

        Thread th = new Thread() {

            public synchronized void run() {
                for (int i = 0; i < 20; i++) {
                    try {
                        Thread.sleep(1000);

                        System.out.print(i + "\n" + "..");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }

        };

        th.start();
        Thread y = new Thread();
        y.start();

    }
}

Error: User Rate Limit Exceeded

public class Mythread3 {
    public static void main(String... a) {

        Thread th = new Thread() {

            public synchronized void run() {
                for (int i = 0; i < 20; i++) {
                    try {
                        Thread.sleep(1000);

                        System.out.print(i + "\n" + "..");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }

        };

        Thread th1 = new Thread() {

            public synchronized void run() {
                for (int i = 0; i < 20; i++) {
                    try {
                        Thread.sleep(1000);

                        System.out.print(i + "\n" + "..");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }

        };
        th.start();
        try {
            th.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        th1.start();

    }
}

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded assylias
Error: User Rate Limit Exceeded Marko Topolnik
Error: User Rate Limit Exceeded dghtr
Error: User Rate Limit Exceeded dghtr
Error: User Rate Limit ExceededareError: User Rate Limit Exceeded Jon Skeet

Ваш Ответ

3   ответа
10

 Runnable run = new Runnable() {
    public void run() {
        try {
            for (int i = 0; i < 20; i++) {
                Thread.sleep(1000);
                System.out.print(i + "\n" + "..");
            }

        } catch (InterruptedException e) {
            System.out.println(" interrupted");
        }
    }
 };
 new Thread(run).start();
 new Thread(run).start();

1

package com.tej.threads;

public class MyThread3 {
public static void main(String... a) {

    Thread th = new Thread() {

        public synchronized void run() {
            for (int i = 0; i < 20; i++) {
                try {

                    System.out.println(i + "\t" + ".." + Thread.currentThread().getId());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    };
    Thread th1 = new Thread() {

        public synchronized void run() {
            for (int i = 50; i < 70; i++) {
                try {

                    System.out.println(i + "\t" + ".."+ Thread.currentThread().getId());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    };
    th.start();
    try {
        th.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    th1.start();
    System.out.println(th.getClass()+ " " + th.getClass().getSuperclass());
    for(int i=0;i<20;i++)
    {
        System.out.println("i am main Thread");
    }


  }
}

0   ..8
1   ..8
2   ..8
3   ..8
4   ..8
5   ..8
6   ..8
7   ..8
8   ..8
9   ..8
10  ..8
11  ..8
12  ..8
13  ..8
14  ..8
15  ..8
16  ..8
17  ..8
18  ..8
19  ..8
class com.tej.threads.MyThread3$1 class java.lang.Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread  
i am main Thread
i am main Thread 
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
50  ..9
51  ..9
52  ..9
53  ..9
54  ..9
55  ..9
56  ..9
57  ..9
58  ..9
59  ..9
60  ..9
61  ..9
62  ..9
63  ..9
64  ..9
65  ..9
66  ..9
67  ..9
68  ..9
69  ..9

Note:

1

Runnable

public class ThreadLauncher {
    public static void main(String[] args) {
         Thread job1 = new Thread(new Job1());
         Thread job2 = new Thread(new Job2());
         job1.start();
         job2.start();
    }
}

public class Job1 implements Runnable {
    @Override
    public void run() {
         // Do some stuff
    }
}

public class Job2 implements Runnable {
    @Override
    public void run() {
         // Do some other stuff
    }
}

ThreadPoolExecutor

Похожие вопросы