在此示例中,当前线程是哪个?

时间:2019-04-28 00:28:56

标签: java multithreading

我不确定Thread.sleep(1000)指的是哪个线程,因为两个线程都与主线程一样运行。

我尝试过在线搜索答案,但似乎找不到任何地方。

Checker: process(clock) begin
    if(rising_edge(clock)) then
        if(BTN_CNT = 1) then
                case morse_code(0 downto 0) is
                        when "0" => LED_16SEGLETTER <=      "00110000011111111"; --E
                        when "1" => LED_16SEGLETTER <=      "00111111111011011"; --T
                        when others => LED_16SEGLETTER <=   "00000000000000001"; --something rando

                end case;
        else if(BTN_CNT = 2) then
                case morse_code(1 downto 0) is
                        when "00" => LED_16SEGLETTER <=     "00110011111011011"; --I
                        when "01" => LED_16SEGLETTER <=     "00001100001111111"; --A
                        when "10" => LED_16SEGLETTER <=     "11001100110110111"; --N
                        when "11" => LED_16SEGLETTER <=     "11001100110101111"; --M
                        when others => LED_16SEGLETTER <=   "00000000000000001"; --something weird
                end case;
        else if(BTN_CNT = 3) then
                case morse_code(2 downto 0) is
                        when "000" => LED_16SEGLETTER <=    "00100010001111111"; --S
                        when "001" => LED_16SEGLETTER <=    "11000000111111111"; --U
                        when "010" => LED_16SEGLETTER <=    "00011100001110111"; --R
                        when "011" => LED_16SEGLETTER <=    "11001100111110101"; --W
                        when "100" => LED_16SEGLETTER <=    "00000011111011011"; --D
                        when "101" => LED_16SEGLETTER <=    "11111100011100111"; --K
                        when "110" => LED_16SEGLETTER <=    "00100000101111111"; --G
                        when "111" => LED_16SEGLETTER <=    "00000000111111111"; --O
                        when others => LED_16SEGLETTER <=   "00000000000000001"; --something weird

                end case;
        else if(BTN_CNT = 4) then
                case morse_code(3 downto 0) is
                        when "0000" => LED_16SEGLETTER <=   "11001100001111111"; --H
                        when "0001" => LED_16SEGLETTER <=   "11111100111101101"; --V
                        when "0010" => LED_16SEGLETTER <=   "00111100011111111"; --F
                        when "0100" => LED_16SEGLETTER <=   "11110000111111111"; --L
                        when "0110" => LED_16SEGLETTER <=   "00011100001111111"; --P
                        when "0111" => LED_16SEGLETTER <=   "11000001111111111"; --J
                        when "1000" => LED_16SEGLETTER <=   "00000011101011011"; --B
                        when "1001" => LED_16SEGLETTER <=   "11111111110100101"; --X
                        when "1010" => LED_16SEGLETTER <=   "00110000111111111"; --C
                        when "1011" => LED_16SEGLETTER <=   "11011110001111011"; --Y
                        when "1100" => LED_16SEGLETTER <=   "00110011111101101"; --Z
                        when "1101" => LED_16SEGLETTER <=   "00000000111110111"; --Q
                        when others => LED_16SEGLETTER <=   "00000000000000001"; --something weird
                end case;
        else if(BTN_CNT = 5)then
                case morse_code(4 downto 0) is
                        when "11111" => LED_16SEGLETTER <=  "00000000111101101"; --0
                        when "01111" => LED_16SEGLETTER <=  "11001111111101111"; --1
                        when "00111" => LED_16SEGLETTER <=  "00010001001111111"; --2
                        when "00011" => LED_16SEGLETTER <=  "00000011101111111"; --3
                        when "00001" => LED_16SEGLETTER <=  "11001110001111111"; --4
                        when "00000" => LED_16SEGLETTER <=  "00100010001111111"; --5
                        when "10000" => LED_16SEGLETTER <=  "01100000001111111"; --6
                        when "11000" => LED_16SEGLETTER <=  "00001111111111111"; --7
                        when "11100" => LED_16SEGLETTER <=  "00000000001111111"; --8
                        when "11110" => LED_16SEGLETTER <=  "00000110001111111"; --9
                        when others => LED_16SEGLETTER <=   "00000000000000001"; --something weird
                end case;
        else if(BTN_CNT = 6) then
                case morse_code(5 downto 0) is
                        when "010101" => LED_16SEGLETTER <= "11111111111111110"; --FullStop
                        when "110011" => LED_16SEGLETTER <= "11111111111110111"; --Comma
                        when "001100" => LED_16SEGLETTER <= "00011111101111010"; --Query
                        when others => LED_16SEGLETTER <=   "00000000000000001"; --something weird
                end case;
        else 
                LED_16SEGLETTER <= "00000000000000001";
        end if;
    end if;
end process;

end arc;

2 个答案:

答案 0 :(得分:0)

thread不能使其他thread进入睡眠状态。 Thread.sleep()将始终使当前thread进入睡眠状态。即使您调用了ob1.t.sleep(),您仍将通过static sleep(long millis)的实例来调用Thread方法,对于您当前所在的thread, 。

我再说一遍,您无法从thread进入其他thread睡眠状态。

是的,您说的对:

  

两个线程与主线程都在运行。

但是main方法在哪个线程中?是ob1.t还是ob2.t?不,它是主要的thread,这意味着它将是正在睡觉的主要thread

答案 1 :(得分:-2)

在Java程序中,运行main(String[] args)函数的线程是主线程。当您调用函数Thread.sleep(long)时,将休眠的线程是调用该函数的线程。在下面看看这个例子。

public class ThreadSleepTest {

   public static void main(String[] args) {
    new Thread(() -> {
        System.out.println("oneSecondSleeper will sleep for 1 seconds");
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
            ex.printStackTrace();
        }
        System.out.println("oneSecondSleeper is awake now!");
    }).start();
    new Thread(() -> {
        System.out.println("twoSecondSleeper will sleep for 2 seconds");
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
            ex.printStackTrace();
        }
        System.out.println("twoSecondSleeper is awake now!");
    }).start();
    System.out.println("mainThread will sleep for 3 seconds");
    try {
        Thread.sleep(3000L);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        ex.printStackTrace();
    }
    System.out.println("mainThread is awake now!");
   }
}

主线程将创建两个子线程,分别休眠一秒钟和两秒钟。创建子线程后,主线程将休眠3秒钟。