线程

2020-02-10 15:30:16 浏览数 (1)

1.程序执行流的最小单元

2.线程组成:线程ID、当前指令指针PC、寄存器集合和堆栈组成

3.与同属一个进程的其他线程共享进程所拥有的资源

代码语言:javascript复制
package thread;

public class threadlearn extends Thread{
    public static void main(String[] args) throws InterruptedException{
        String messages[] = {"消息1","消息2"};
        for (int i = 0 ;i<messages.length;i  ){
            try {
                Thread.sleep(3000);
            }catch(InterruptedException e){
                return;
            }
            System.out.println(messages[i]);
        }
    }
}
代码语言:javascript复制
package thread;

public class runnablelearn implements Runnable{
    public void run(){
        int messages[] = {2,2,2,2,2,2,2,2,1,1,11,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        for (int i = 0 ;i<messages.length;i  ){
            System.out.println(messages[i]);
        }
    }

    public static void main(String[] args) throws Exception{
        Runnable r = new runnablelearn();
        Thread t = new Thread(r);
        Thread t2 = new Thread(r);
        t.start();
        t.join();
        t2.start();
        t2.join();
    }
}

学习目标: 定义并启动一个线程、线程暂停、线程中断、join方法、死锁

0 人点赞