Experiment 6 Java multithreading

table of Contents

1. The purpose of the experiment

2. Experimental code

1. Create two threads by inheriting the method of the Thread class, specify the name of the thread in the Thread construction method, and print the names of the two threads.

2. Create a new thread by implementing the Runnable interface, requiring the main thread to print "main" 100 times and the new thread to print "new" 50 times.

3. Simulate three teachers sending out 80 study notebooks at the same time. Only one notebook is issued each time. Each teacher is equivalent to a thread.

4. Write the interface as shown in Figure 6-1, when the program is running:

One word per text


 

 

1. The purpose of the experiment

1. Master the creation and startup of Java multi-threading, the two common creation methods of multi-threading and their differences;

2. Grasp the life cycle of multithreading and the five basic states, namely the new state ( New ), the ready state ( Runnable ), the running state ( Running ), the blocked state ( Blocked ), and the dead state ( Dead );

3. Master the main methods that cause Java thread blocking, such as: jion() method, sleep() method, yeild() method;

4. Master thread safety and its solution mechanisms, such as synchronization methods, synchronization code blocks, etc.

2. Experimental code

1. Create two threads by inheriting the method of the Thread class, specify the name of the thread in the Thread construction method, and print the names of the two threads.

package 作业练习.test6;
public class Study_1 extends Thread {
    public Study_1(String name) {
        super(name);
    }
    public void run() {
        System.out.println(this.getName());
    }
    public static void main(String[] args) {
        new Study_1("Thread1").start();
        new Study_1("Thread2").start();
    }
}

2. Create a new thread by implementing the Runnable interface, requiring the main thread to print "main" 100 times and the new thread to print "new" 50 times.

package 作业练习.test6;

public class Study_2 implements Runnable {
    public void run() {
        for (int i = 0; i < 50; i++) {
            System.out.println("new");
        }
    }
    public static void main(String[] args) {
        new Thread(new Study_2()).start();
        for (int i = 0; i < 100; i++) {
            System.out.println("main");
        }
    }
}

3. Simulate three teachers sending out 80 study notebooks at the same time , and only one notebook at a time. Each teacher is equivalent to a thread.

package 作业练习.test6;
public class Study_3 implements Runnable {
    private int number = 80;  //总共80份学习笔记
    private int count = 0;    //发第几份
    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (number <= 0) {
                    break;
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                number--;
                count++;
                System.out.println(Thread.currentThread().getName() + "正在发第" + count + "份学习笔记,还有" + number + "份学习笔记。");
            }
        }
    }
    public static void main(String[] args) {
        Study_3 homeWork3 = new Study_3();
        new Thread(homeWork3, "老师甲").start();
        new Thread(homeWork3, "老师乙").start();
        new Thread(homeWork3, "老师丙").start();
    }
}

4. Write the interface shown in Figure 6-1 , when the program is running:

( 1 ) A letter is randomly displayed in the display letter area every two seconds (as shown in the figure, the displayed letter is " g ");

( 2 ) The user enters the corresponding letter in the text box. If the input is correct, it will get 1 point, otherwise it will get 0 point;

( 3 ) Display the cumulative score of the user in the score column.

package 作业练习.test6;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;

import java.awt.*;
import java.awt.event.KeyEvent;

public class Text4 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Label l1 = new Label("显示字母:");
        TextField l2 = new TextField("A");
        l2.setEditable(false);
        l2.setPrefWidth(30.0);//文本框大小
        Label l3 = new Label("输入所显示的字母(回车)");
        TextField l4 = new TextField();
        Label l5 = new Label("得分");
        Label l6 = new Label("0");
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    char str;
                    str = (char)(Math.random()*26+'a');
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    String s = String.valueOf(str);
                    l2.setText(s);
                }
            }
        }).start();

        l4.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent e) {
                if (e.getCode()== KeyCode.ENTER){
                    String text1 = l2.getText();
                    String text2 = l4.getText();
                    String text3 = l6.getText();
                    int i = Integer.parseInt(text3);
                    if (text1.equals(text2)){
                        i+=1;
                        l6.setText(String.valueOf(i));

                    }
                }
                l4.clear();
            }
        });


        FlowPane flowPane = new FlowPane();    //添加面板
        flowPane.getChildren().addAll(l1,l2,l3,l4,l5,l6);    //吧空控件添加到面板上
        Scene scene = new Scene(flowPane);   //创建场景
        stage.setScene(scene);   //吧场景添加到舞台
        stage.setWidth(900);
        stage.show();

    }
}

One word per text

Some things can only be cherished if you have regrets!

Guess you like

Origin blog.csdn.net/weixin_47723732/article/details/112951159