[java] Print Roman letters and Greek letters (multi-threaded)

[Task introduction]

1.Task description

One thread prints the Roman letters a through z on the screen, and the other thread prints the Greek letters α through ω on the screen
. Create the above two threads in the test program and start execution, printing Roman letters and Greek
letters alternately.

Requirements:
1. Use inheritance to implement multi-threading when printing Roman letters. Print "Rome" before printing each letter.
2. Printing Greek letters uses an interface to implement multi-threading. Print "Greece" before printing each letter.

【mission target】

 Learn to analyze the implementation ideas of the program "printing Roman letters and Greek letters".

 Independently complete the source code writing, compilation and operation of "printing Roman letters and Greek letters" based on ideas.

 Master the implementation method of Java multi-threading.

[Implementation ideas]

1. Write a Roman class for printing Roman letters, inherit from Thread, and override the run method. Use a for loop
to print "Roman:" and the corresponding letters. Tip: loop part for (char c='a'; c<='z'; c++)

2. Write the Greek class for printing Greek letters, implement the Runnable interface, and override the run method. Use for

Loops to print "Greek:" and the corresponding letters. Tip: loop part for (char c='α '; c<='ω '; c++)

3. Write the ThreadDemo class, use the above two thread classes to create thread objects in the main method, and start execution.

[Implementation code]

package practice.exp12;

public class Roman extends Thread {
    
    
    private char c;
    public void run(){
    
    
        for(char c='a';c<='z';c++) {
    
    
            System.out.println("罗马:" + c);
        }
    }
}

package practice.exp12;

public class Greek implements Runnable{
    
    
    private char c;
 public void run(){
    
    
     for (char c='α'; c<='ω'; c++){
    
    
         System.out.println("希腊:"+c);
     }
 }
}

package practice.exp12;

public class ThreadDemo {
    
    
    public static void main(String[] args) {
    
    
        Roman r = new Roman();
        r.start();

        Greek g = new Greek();
        Thread thread = new Thread(g);
        thread.start();

    }
}

【operation result】
Insert image description here

Guess you like

Origin blog.csdn.net/m0_52703008/article/details/126209569