JAVA 线程基础(上)

1、进程是操作系统的一个任务是一块包含了某些资源的内存区域操作系统利用进程把它的工作划分为一些功能单元

进程中所包含的一个或多个执行单元称为线程

2、一个进程至少一个线程,线程通常用于在一个程序中需要同时完成多个任务

3、多个线程同时运行只是我们感官上的一种表现,线程是以并发运行的 并发顾名思义就是线程在计算机cpu上运行的时间片段 微观上走走停停宏观上都在运行 这种现象就是并发

//创建线程1

public class textThread extends Thread{

  public void run(){

    System.out.println("我是线程1");

  }

}

//启动线程 先实例化线程

textThread t1 = new textThread();

t1.start();

//创建线程2

public class textRunnable implements Runnable{

  public void run(){

    System.out.println("我是线程2");

  }

}

//启动线程 实例化线程

textRunnable t2 = new textRunnable();

Thread thread = new Thread(t2);//接收线程体

thread.start();

//创建匿名线程1

Thread thread = new Thread(){

  public void run(){

    System.out.println("我是线程3");

  }

}

thread.start(); 

//创建匿名线程2

Runnable runnable = new Runnable(){

  public void run(){

    System.out.println("我是线程4");

  }

}

Runnable  trun = new Runnable();

Thread tred = new Thread(trun); 

tred .start();

 

posted @ 2016-05-17 23:21  焚情  阅读(170)  评论(0编辑  收藏  举报