java基础的核心技术:多线程(一)

来源:互联网 发布:软壳冲锋衣 知乎 编辑:程序博客网 时间:2024/05/17 05:05

1、程序、进程、线程的概念

2、java中多线程的创建和使用(重点)

2.1、继承Thread类与实现Runnable接口

2.2、Thread类的主要方法

2.3、线程的调度与设置优先级

3、线程的生命周期

4、线程的同步(重点)

5、线程的通信 


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------


一、基本概念: 程序--进程--线程

1) 程序(Program): 是为完成特定任务、用某种语言编写的一组指令的集合。即指一段静态的代码,静态对象。

2) 进程(process):是程序的一次执行过程,或是正在运行的一个程序,动态过程:有它自身的产生、存在和消亡的过程。

-->如:运行中的QQ,运行中的MP3播放器

-->程序是静态的,进程是动态的

3) 线程(Thread):进程可进一步细化为线程,是一个程序内部的一条执行路径。

-->若一个程序可同一时间执行多个线程,就是支持多线程的

4) 每个Java程序都有一个隐含的主线程:main()方法


问题:何时需要多线程?

程序需要同时执行两个或多个任务

程序需要实现一些需要等待的任务时,如用户输入、文件读写操作、网络操作、搜索等 。

需要一些后台运行的程序时。


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

二、创建多线程

什么是单线程?

package com.attest.java1;//单线程 主线程public class TestMain {public static void main(String[] args) {method2("atTestThread");}public static void method1(String str){System.out.println("method1.....");System.out.println(str);}public static void method2(String str){System.out.println("method2.....");method1(str);}}
运程的结果:无论你怎么运程,结果只有一个:




--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

多线程的创建和启动的步骤:


1、Java语言的JVM允许程序运行多个线程,它通过java.lang.Thread类来实现。

2、Thread类的特性

2.1、每个纯种都是通过某个特定Thread对象的run()方法来完成操作的。经常把run()方法的主体称为线程体。

2.2、通过该Thread对象的start()方法来调用这个线程


看一下Java-jdk的api是怎么说的(这是从java api 1.7.0摘抄而来的)


java.lang类 Threadjava.lang.Object java.lang.Thread 所有已实现的接口: Runnable 直接已知子类: ForkJoinWorkerThread --------------------------------------------------------------------------------public class Threadextends Objectimplements RunnableA thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon. When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs: The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place. All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method. There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows: --------------------------------------------------------------------------------     class PrimeThread extends Thread {         long minPrime;         PrimeThread(long minPrime) {             this.minPrime = minPrime;         }         public void run() {             // compute primes larger than minPrime              . . .         }     } --------------------------------------------------------------------------------The following code would then create a thread and start it running:      PrimeThread p = new PrimeThread(143);     p.start(); The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started. The same example in this other style looks like the following: --------------------------------------------------------------------------------     class PrimeRun implements Runnable {         long minPrime;         PrimeRun(long minPrime) {             this.minPrime = minPrime;         }         public void run() {             // compute primes larger than minPrime              . . .         }     } --------------------------------------------------------------------------------The following code would then create a thread and start it running:      PrimeRun p = new PrimeRun(143);     new Thread(p).start(); Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it. Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.


例子:创建线程

package com.attest.java1;/* * 创建一个子线程,完成1-100之间自然数的输出。同样的,主线程执行同样的操作 * 创建多线程的第一种方式,继承java.lang.Thread类 *///1、创建一个继承于Thread的子类class SubThread extends Thread {// 重写Thread类的run( )方法,方法内实现此子线程想要完成的功能@Overridepublic void run() {for (int i = 0; i <= 100; i++) {System.out.println(Thread.currentThread().getName() + " : " + i);}}}public class TestThread {public static void main(String[] args) {// 3、创建一个子类的对象SubThread subT = new SubThread();// 4、调用线程的start( ),启动此线程,调用相应的run( )方法subT.start();for (int i = 0; i <= 100; i++) {System.out.println(Thread.currentThread().getName() + " : " + i);}}}
结果如下:







 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 0