多线程
进程
-
进程:(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。在进程是程序的基本执行实体,在当代面向线程设计的计算机结构中,进程是线程的容器,程是程序的实体。
-
多线程:是指从软件或者硬件上实现多个线程并发执行的技术,是为了提高CPU等资源的使用率而采用的一种新技术。将一个执行活动-进程,拆分为多个小的执行片段,片段之间是共享数据而完成进程任务,进而提升整体处理性能。
-
Java程序运行
- Java命令去启动JVM,JVM会启动一个进程,该进程会启动一个主线程。
- JVM的启动是多线程的,因为它最低有两个线程启动了,主线程和垃圾回收线程。
-
进程的实现方式:
-
继承Thread类.
/*for example*/ public final String getName() //获取线程的名称。 public final void setName(String //name):设置线程的名称 System.out.println(Thread.currentThread().getName()); //控制台显示当前指定对象名字。 public class MyThread extends Thread { public MyThread() { } public MyThread(String name){ super(name); } @Override public void run() { for (int x = 0; x < 100; x++) { System.out.println(getName() + ":" + x); } } } public static void main(String[] args) { /*method:the first...*/ MyThread myThread1 = new MyThread(); MyThread myThread2 = new MyThread(); myThread1.setName("Tom"); myThread2.setName("Jack"); myThread1.start(); myThread2.start(); /*method:the second...*/ MyThread Thread1 = new MyThread("Tom"); MyThread Thread2 = new MyThread("Jack"); Thread1.start(); Thread2.start();
-
实现Runnable接口.
public class MyRunnable implements Runnable { @Override public void run() { for (int x = 0; x < 10; x++) { System.out.println(Thread.currentThread().getName() + ":" + x); } } } public class MyRunnable { public static void main(String[] args) { MyRunnable my = new MyRunnable(); /*method:the first...*/ Thread Thread1 = new Thread(my); Thread Thread2 = new Thread(my); Thread1.setName("Tom"); Thread2.setName("Jack"); /*method:the second...*/ Thread Thread1 = new Thread(my, "Tom"); Thread Thread2 = new Thread(my, "Jack"); Thread1.start(); Thread2.start(); } }
-
Callable&ExecutorService.
/*for example*/ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Callable; /*Callable:是带泛型的接口,这里指定的泛型其实是call()方法的返回值类型*/ @SuppressWarnings("rawtypes") class MyCallable implements Callable { /*Callable JDK1.5*/ @Override public Object call() throws Exception { for (int x = 0; x < 50; x++) { System.out.println(Thread.currentThread().getName() + ":" + x); } return null; } } public class CallableDemo { public static void main(String[] args) { /*创建线程池对象*/ ExecutorService pool = Executors.newFixedThreadPool(2); pool.submit(new MyCallable()); pool.submit(new MyCallable()); pool.shutdown(); } }
-
-
线程的调度和优先级问题
- 线程的调度:分时调度,时间片轮转机制,而Java是采用抢占式调度。
- 获取&设置线程优先级:Java中线程运行的优先级自高到底:10级->1级,默认5级;需要说明的是不是运行的级别越高就保证一定先执行该线程完该线程再执行其他低优先级的线程,只是保证其具有很大的可能有优先级。
-
线程的控制
-
休眠线程
/*for example*/ /* public static void sleep(long millis) */ import java.util.Date; public class ThreadSleep extends Thread { @Override public void run() { for (int x = 0; x < 20; x++) { System.out.println(getName() + ":" + x + ",Date:" + new Date()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } /* 解决直接重命名线程名称.*/ /*public ThreadSleep(String name){ super(name); }*/ } public class ThreadSleepDemo { public static void main(String[] args) { ThreadSleep Aa = new ThreadSleep(); ThreadSleep Bb = new ThreadSleep(); ThreadSleep Cc = new ThreadSleep(); /*ThreadSleep Dd = new ThreadSleep("Smith");*/ Aa.setName("Tom"); Bb.setName("John"); Cc.setName("Jack"); Aa.start(); Bb.start(); Cc.start(); } }
-
加入线程
/*实现join功能*/ package CompareTools; class sleepThread extends Thread { @Override public void run() { for (int i = 0; i < 10; ++i) { try { sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(getName() + ":" + i); } } public sleepThread() { } public sleepThread(String name) { super(name); }} public class TestFiles { public static void main(String[] args) { // TODO Auto-generated method stub sleepThread spOne = new sleepThread("Tom"); sleepThread spTwo = new sleepThread("Jack"); sleepThread spThree = new sleepThread("Smith"); spThree.start(); try { spThree.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } spOne.start(); spTwo.start(); } } /*Smith执行完成才继续执行后面的,但若将spThree放到try后面不会有描述的效果。*/
-
礼让线程
package CompareTools; class PriorityThread implements Runnable { @Override public void run() { for (int i = 0; i < 10; ++i) { /* 实现Runnable无法使用Thread的getName函数. */ System.out.println(Thread.currentThread().getName() + ":" + i); Thread.yield(); /*礼让不代表一定是程序严格运行一次就对方执行.*/ } } } public class TestFiles { public static void main(String[] args) { PriorityThr
-