如何在100行中显示线程,每行长度为100个字符

时间:2015-09-12 15:41:00

标签: java multithreading

如何以100行显示线程,每行长度为100个字符?

    public class CustomThread extends Thread{

    private Thread t;
    private String threadName;

    CustomThread( String threadName){
        this.threadName = threadName;
        System.out.println("Creating " +  threadName );
    }
    public void run() {
        System.out.println("Running " +  threadName );
        try {
            for(int i = 0; i < 100; i++) {
                for (int j = 0; j < 100; j++) {
                    System.out.println(threadName);
                    Thread.sleep(50);
                }
                System.out.println("\n");
            }
        } catch (InterruptedException e) {
            System.out.println("Thread " +  threadName + " interrupted.");
        }
        System.out.println("Thread " + threadName + " exiting.");
    }

    public void start ()
    {
        System.out.println("Starting " +  threadName );
        if (t == null)
        {
            t = new Thread (this, threadName);
            t.start ();
        }
    }
}

当每行长度为100个字符时,如何显示100行。

我的主要人物:

public class Main {
    public static void main(String[] args) {

        CustomThread T1 = new CustomThread("1");
        T1.start();

       }
}

这个角色是&#34; 1&#34;。但是如何在100个字符上显示100行,该线程?我的代码不起作用。

1 个答案:

答案 0 :(得分:4)

为什么不直接命名自己的线程?

    Thread.currentThread().setName("Bob");
    System.out.println(Thread.currentThread().getName());

无论如何,要打印行,请更改

System.out.println(threadName);

System.out.print(threadName);

println()始终包含换行符