RE:JAVA学习-了解线程

1.什么是进程(一块包含了某些资源的内存区域)
操作系统中运行的一个任务
2.什么是线程(进程中所包含的一个或多个执行单元)
概念:进程的一个顺序执行流
使用: 1.(通常)在一个程序需要同时完成多个任务时
2.用于单一线程时,使用多线程可以更快(如下载文件)

3.进程和线程的区别
3.1 线程只能归属于一个进程(当操作系统创建一个进程后,该进程会自动申请一个主线程(首要线程))
3.2 一个进程至少有一个线程
3.3 (执行过程中的区别)每个独立的线程有一个程序运行的入口、顺序执行序列和程序的出口(线程不能独立执行,依附在应用程序中)

4.多线程并发原理
OS(操作系统)将时间划分为很多时间片段(时间片),尽可能均匀分配给每一个线程,获取时间片段的线程则运行,其他则等待。(在微观上并不是同时发生)
但在宏观上,则同时运行(称作并发)

5.创建Thread(线程)并启用
5.1 继承Thread 线程类,并重写其run()方法,并在main函数中用start()方法调用(重用性差)
class MyThread1 extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<100;i++){
System.out.println(“who?”);
}
super.run();
}
}
5.2实现Runnable接口并重写run方法(可以减少耦合度)
class MyRunnable1 implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 100; i++) {
System.out.println(“who???”);
}
}
}
5.3使用(匿名)内部类创建
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<100;i++){

            System.out.println("你是谁啊?");
        }
        super.run(); 
    }
}.start();

5.4实现Callable<R>接口,重写其中的call方法

6.有关线程操作
6.1 static Thread currentThread()
获取运行这个方法的线程
Thread th=Thread.currentThread();

public class Thread_currentThread {
    public static void main(String[] args) {
        //获取运行main方法的线程
        Thread th=Thread.currentThread();
        System.out.println("运行main方法的线程是:"+th);
        dosome();
        Thread t=new Thread(){
            @Override
            public void run() {
                // TODO Auto-generated method stub
                Thread t=Thread.currentThread();
                System.out.println("自定义线程:"+t);
                dosome();
                super.run();
            }

        };
        t.start();
    }

    public static void dosome(){
        Thread t=Thread.currentThread();
        System.out.println("运行dosome方法的线程是:"+t);
    }
}

6.2线程优先级
理论上,线程优先级越高的线程获取CPU时间片的次数越多
线程的优先级有10个等级,分别用整数1-10表示,5为默认
Thread max=new Thread(“max”);
Thread min=new Thread(“min”);
max.setPriority(Thread.MAX_PRIORITY);
min.setPriority(Thread.MIN_PRIORITY);

6.3 static void sleep(long ms)
    该方法会将运行当前方法的线程阻塞指定毫秒
    SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");

    while(true){
        Date date=new Date();
        String s=sdf.format(date.getTime());
        System.out.println(s);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

6.4其他
getId()
getName()
getPriority() 获取优先级
isAlive()
isDaemon() 是否为守护线程
isInterrupted() 是否被中断
/**
 * 获取线程信息的相关方法
 *
 */
public class Thread_info {
    public static void main(String[] args) {
        Thread t=Thread.currentThread();
        //获取线程标识
        long id=t.getId();
        System.out.println("id:"+id);//id:1

        System.out.println(t.getName());//main

//      int priority=t.getPriority();
        System.out.println(t.getPriority());//5
//      t.interrupt();
        //线程是否活着
        System.out.println(t.isAlive());//true
        //是否为守护线程
        System.out.println(t.isDaemon());//false
        //是否被中断
        System.out.println(t.isInterrupted());//false
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值