进程和线程
进程和程序区别:
1、进程是程序的一次运行活动,属于一种动态概念
2、一个进程可以执行一个或多个程序
3、程序可以作为一种软件资源长期保存着,而进程是一次执行过程,只是短暂的。
进程具有并发行和不确定性
进程的结构
进程通常由三个部分组成:程序、数据集合、进程控制块即PCB
线程
线程本身不能独立运行,必须在进程中执行。
一个进程内部包含多个顺序控制流,或者并发执行多种运算,称为多线程。
创建进程消耗大,因为每个进程都有独立的数据和代码空间;进程间通信不方便,比如消息机制。
进程内的同一类线程可以共享代码和数据空间,切换开销小,灵活。
java定义的线程概念模型
一个线程分为3个部分:虚拟CPU即Thread类,虚拟CPU执行代码和数据。
java构造线程的方法:
1、继承Tread,重写run方法;
2、把线程体从Thread类中独立出来,形成单独的线程目标对象,实现Runnable接口和run方法;(实现Runnable接口的类创建的对象称作线程的目标对象)
JDK线程池
1、创建线程目标对象
2、使用Executors创建线程池,返回一个ExecutorService类型的对象
3、使用线程池执行线程目标对象,exec.execute(run)
4、最后结束线程池exec.shutdown()
public class Runner implements Runnable {
int index =0;
public Runner(int index){
this.index = index;
}
@Override
public void run() {
long time = (long)(Math.random()*1000);
System.out.println("线程:"+Thread.currentThread().getName()
+"(目标对象"+index+")"+":sleeping:"+time+"ms");
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestThreadPool {
public static void main(String[] args) {
//线程池中创建2个对象
ExecutorService exec = Executors.newFixedThreadPool(2);
for(int index=0; index<100; index++) {
Runnable run = new Runner(index);
//执行线程目标对象
exec.execute(run);
}
//shutdown
exec.shutdown();
}
}
java线程的基本控制
1、Thread.sleep()使当前线程执行暂停一段指定的时间。该方法不会放弃除CPU之外的其他资源。
并不能保证sleep睡眠时间的精确性,可以测试一下:
public class SleepTest {
public static void main(String[] args) {
String[] arg = {"one", "two", "three", "four"};
long start = System.nanoTime();
for(int i=0; i<arg.length; i++) {
System.out.println(arg[i]);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long end = System.nanoTime();
System.out.println("总时间:"+(end-start)/1000000);
}
}
2、Thread.join()等待另一个线程结束
public class JoinTest extends Thread {
static int result=0;
public JoinTest(String name){
super(name);
}
public static void main(String[] args) {
System.out.println("执行主线程...");
Thread t = new JoinTest("计算线程");
t.start();
System.out.println("result:" + result);
long start = System.nanoTime();
try {
//主线程等待计算线程执行完以后再执行
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.nanoTime();
System.out.println((end-start)/1000000+"ms以后result="+result);
}
@Override
public void run() {
System.out.println(this.getName()+"开始计算...");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
result = (int)(Math.random()*10000);
System.out.println(this.getName()+"结束计算...");
}
}
3、Thread.interrupt()取消线程
public class InterruptTest2 extends Thread {
static int result=0;
public InterruptTest2(String name){
super(name);
}
public static void main(String[] args) {
System.out.println("执行主线程...");
Thread t = new InterruptTest2("计算线程");
t.start();
System.out.println("result:" + result);
long start = System.nanoTime();
try {
t.join(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.nanoTime();
t.interrupt();
System.out.println((end-start)/1000000+"ms以后result="+result);
}
@Override
public void run() {
System.out.println(this.getName()+"开始计算...");
for(int i=0; i<100000; i++) {
result++;
if(Thread.interrupted()) {
System.out.println(this.getName()+"被中断,结束");
return;
}
}
// result = (int)(Math.random()*10000);
System.out.println(this.getName()+"结束计算...");
}
}
4、Thread.stop()不建议使用