进程和线程:进程可以理解为是正在执行的程序。线程是最小的分派代码单元,程序可以一次执行多项任务。
线程的状态:running(运行),read to run(准备就绪),suspended(挂起),resumed(继续执行),blocked(阻塞状态),terminated(终止)。
示例代码:
- class MyThread implements Runnable{ //实现Runnable接口
- String thrdName;
- MyThread(String name){ //构造
- thrdName=name;
- }
- public void run(){ //线程开始执行
- System.out.println(thrdName+" starting.");
- try{
- for(int i=0;i<10;i++){
- Thread.sleep(400); //线程休眠时间(毫秒)
- System.out.println("In "+thrdName+"i is "+i);
- }
- }catch(InterruptedException e){
- System.out.println(e);
- }
- System.out.println(thrdName+" termination.");
- }
- }
- class UseThreads{
- public static void main(String[] args){
- System.out.println("Main thread starting.");
- MyThread mt=new MyThread("Child #1"); //创建一个可运行的对象
- Thread newThread=new Thread(mt); //构造一个线程
- newThread.start(); //开始运行线程
- for(int i=0;i<50;i++){
- try{
- Thread.sleep(100); //休眠时间
- }catch(InterruptedException e){ //捕获异常
- System.out.println(e);
- }
- }
- System.out.println("Main thread ending");
- System.out.println("Child #1×Ô¼ºÔËÐÐ");
- mt.run(); //直接调用run()方法
- }
- }
- ------------------------------------------------------------------------------------------------------
- 创建多线程:
- class MyThreadsss implements Runnable{ //实现Runnable接口
- Thread thrd; //Thread对象
- MyThreadsss(String name){ //构造
- thrd=new Thread(this,name); //创建时运行
- thrd.start();
- }
- public void run() { //run()方法
- System.out.println(thrd.getName()+" starting.");
- try {
- for(int i=0;i<10;i++) {
- Thread.sleep(400); //线程休眠
- System.out.println("In "+thrd.getName()+"i is "+i);
- }
- }catch(InterruptedException e) {
- System.out.println(e);
- }
- System.out.println(thrd.getName()+" terminating.");
- }
- }
- public class MoreThreads {
- public MoreThreads() {
- }
- public static void main(String[] args) {
- System.out.println("Main thread starting.");
- MyThreadsss mt1=new MyThreadsss("Child#1"); //多个子线程
- MyThreadsss mt2=new MyThreadsss("Child#2");
- MyThreadsss mt3=new MyThreadsss("Child#3");
- do {
- try {
- Thread.sleep(100); //线程休眠
- } catch (InterruptedException e) {
- e.printStackTrace();
- System.out.println("Main thread interrupted.");
- }
- }while(mt1.thrd.isAlive()||mt2.thrd.isAlive()||mt3.thrd.isAlive()); //isAlive()确定线程是否结束,运行返回true,结束返回false
- mt1.thrd.join();//等待,直到指定线程结束
- mt1.thrd.setPriority(Thread.NORM_PRIORITY+2); //设置线程优先级
- mt2.thrd.setPriority(Thread.NORM_PRIORITY-2);
- System.out.println("Main thread ending.");
- }
- }
- ------------------------------------------------------------------------------------------------
- 线程通信:notify() wait()之间的通信
- class TickTock{
- String state;
- synchronized void tick(boolean running) { //synchronized修饰方法,方法同步
- if(!running) {
- state="ticked";
- notify(); //通知其他等待的线程
- return;
- }
- System.out.println("Ticked ");
- try {
- Thread.sleep(500);
- } catch (InterruptedException e1) {
- e1.printStackTrace();
- }
- state="ticked";
- notify(); //通知其他等待的线程
- try {
- while(!state.equals("tocked")) {
- wait(); //等待
- }
- }catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- synchronized void tock(boolean running) { //同步方法
- if(!running) {
- state="tocked";
- notify(); //通知其他等待的线程
- return;
- }
- System.out.println("Tocked");
- try {
- Thread.sleep(500); //休眠
- } catch (InterruptedException e1) {
- e1.printStackTrace();
- }
- state="tocked";
- notify(); //通知其他等待的线程
- try {
- while(!state.equals("ticked")) {
- wait(); //等待
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- class MyThreadtc implements Runnable{ //实现Runnable接口
- Thread thrd;
- TickTock ttOb;
- MyThreadtc(String name,TickTock tt){
- thrd=new Thread(this,name); //创建对象时运行
- ttOb=tt;
- thrd.start();
- }
- public void run() { //run()方法
- if(thrd.getName().compareTo("Tick")==0) {
- for(int i=0;i<5;i++) {
- ttOb.tick(true);
- }
- ttOb.tick(false);
- }else {
- for(int i=0;i<5;i++) {
- ttOb.tock(true);
- }
- ttOb.tock(false);
- }
- }
- }
- public class ThreadCom {
- public ThreadCom() {
- }
- public static void main(String[] args) {
- TickTock tt=new TickTock();
- MyThreadtc mt1=new MyThreadtc("Tick",tt);
- MyThreadtc mt2=new MyThreadtc("Tock",tt);
- try {
- mt1.thrd.join(); //等待,知道线程结束
- mt2.thrd.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-------------------------------------------------------------------------------------- - 线程的挂起,继续执行和停止:
- class MyThreadsrs implements Runnable{
- Thread thrd;
- boolean suspended; //为true时,线程挂起
- boolean stopped; //为true时,线程终止
- MyThreadsrs(String name){
- thrd=new Thread(this,name);
- suspended=false;
- stopped=false;
- thrd.start();
- }
- public void run() {
- System.out.println(thrd.getName()+" starting.");
- try {
- for(int i=1;i<1000;i++) {
- System.out.print(i+" ");
- if((i%10)==0) {
- System.out.println();
- Thread.sleep(250);
- }
- synchronized(this) { //同步代码块检查suspended和stopped
- while(suspended) {
- wait();
- }
- if(stopped) break;
- }
- }
- }catch(InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(thrd.getName()+" ending");
- }
- synchronized void mystop() { //stop()方法
- stopped=true;
- suspended=false;
- notify();
- }
- synchronized void mysuspend() { //suspend()方法
- suspended=true;
- }
- synchronized void myresume() { //resume()方法
- suspended=false;
- notify();
- }
- }
- public class Suspend {
- public Suspend() {
- }
- public static void main(String[] args) {
- MyThreadsrs ob1=new MyThreadsrs("My Thread");
- try {
- Thread.sleep(1000);
- ob1.mysuspend();
- System.out.println("Suspending thread.");
- Thread.sleep(1000);
- ob1.myresume();
- System.out.println("Resuming thread.");
- Thread.sleep(1000);
- ob1.mysuspend();
- System.out.println("Suspending thread.");
- Thread.sleep(1000);
- ob1.myresume();
- System.out.println("Resuming thread.");
- Thread.sleep(1000);
- ob1.mysuspend();
- System.out.println("Stopping thread.");
- ob1.mystop();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- try {
- ob1.thrd.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- --------------------------------------------------------------------------------------
- 主线程:
- public class UseMain {
- public UseMain() {
- }
- public static void main(String[] args) {
- Thread thrd;
- thrd=Thread.currentThread(); //主线程
- System.out.println("Main thread is called:"+ thrd.getName()); //获取线程名
- System.out.println("Priority:"+thrd.getPriority()); //获取线程优先级别
- System.out.println();
- System.out.println("Setting name and priority.");
- thrd.setName("Thread #1"); //设置主线程名字
- thrd.setPriority(Thread.NORM_PRIORITY+3); //设置主线程优先级
- System.out.println("Main thread is called:"+ thrd.getName());
- System.out.println("Priority:"+thrd.getPriority());
- System.out.println();
- }
- }