-1

How to display thread in 100 lines, each line length of 100 characters ?

    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 ();
        }
    }
}

How to display 100 lines, when each line length of 100 characters.

My main:

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

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

       }
}

This character is "1". But how to display 100 lines on 100 characters, that thread ? My code is not work.

1 Answers1

4

Why don't you just name your own thread?

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

Anyway, to print your rows, change

System.out.println(threadName);

to

System.out.print(threadName);

println() always include a new line character

Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48