Java并发程序入门介绍攻略
简介
Java并发编程是指在多线程环境下,多个线程之间相互合作、协同工作的编程模型。Java并发编程常常被采用于提高程序运行效率、提升程序响应速度以及提高程序质量等方面。本篇文章将简单介绍Java并发编程的基本知识,包括Java并发编程的常见术语、Java并发编程的核心知识点以及Java并发编程的示例等。
Java并发编程常见术语
- 进程:系统分配资源的最小单元,由程序、数据和进程控制块组成。
- 线程:进程的子单元,是系统调度和分配资源的基本单元,负责执行程序中某个指定的线性序列。
- 并发:指在单个处理器上同时执行多个程序,共享资源, 使这些程序在用户看来就像在同时运行一样。
- 同步:保持两个或多个并发线程在进程内的执行顺序,以达到用户期望的顺序。
- 互斥:两个或多个并发线程在访问共享资源时会产生冲突,需要通过互斥机制来实现访问控制。
Java并发编程的核心知识点
线程创建
Java中有两种创建线程的方式:继承Thread类和实现Runnable接口。
继承Thread类方式的示例代码如下:
class MyThread extends Thread{
public void run(){
System.out.println("继承Thread创建线程");
}
}
public class ThreadDemo{
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
实现Runnable接口方式的示例代码如下:
class MyRunnable implements Runnable{
public void run(){
System.out.println("实现Runnable接口创建线程");
}
}
public class ThreadDemo{
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
}
}
线程安全
在多线程环境下,多个线程访问共享资源时容易出现线程安全问题,为了避免线程安全问题,Java提供了线程同步机制,如synchronized关键字和Lock接口等。
synchronized关键字示例代码如下:
class MyThread extends Thread{
public synchronized void run(){
//同步代码块
System.out.println("继承Thread创建线程");
}
}
public class ThreadDemo{
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
Lock接口示例代码如下:
class MyThread implements Runnable{
private Lock lock = new ReentrantLock();
public void run(){
lock.lock();
try{
//同步代码块
System.out.println("实现Runnable接口创建线程");
}finally{
lock.unlock();
}
}
}
public class ThreadDemo{
public static void main(String[] args) {
MyThread myRunnable = new MyThread();
Thread thread = new Thread(myRunnable);
thread.start();
}
}
示例说明
示例一
在下面的代码中,创建了五个线程,每个线程执行的是相同的方法sendThread,方法内包含了一个同步代码块,此方法在所有线程中共享。
class SendThread extends Thread{
private static int count = 10;
private static final Object lock = new Object();
public void run(){
while(count > 0){
synchronized(lock){
System.out.println(Thread.currentThread().getName() + " send " + count--);
}
}
}
}
public class ThreadDemo{
public static void main(String[] args) {
SendThread sendThread = new SendThread();
Thread t1 = new Thread(sendThread, "Thread-1");
Thread t2 = new Thread(sendThread, "Thread-2");
Thread t3 = new Thread(sendThread, "Thread-3");
Thread t4 = new Thread(sendThread, "Thread-4");
Thread t5 = new Thread(sendThread, "Thread-5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
运行结果如下:
Thread-1 send 10
Thread-1 send 9
Thread-3 send 8
Thread-3 send 7
Thread-1 send 6
Thread-1 send 5
Thread-4 send 4
Thread-4 send 3
Thread-2 send 2
Thread-2 send 1
Thread-5 send 0
示例二
在下面的代码中,创建了10个线程,每个线程执行的是相同的方法printThread,方法内不包含同步代码块,此方法在所有线程中共享。
class PrintThread extends Thread{
private static int count = 0;
public void run(){
while(count <= 20){
System.out.println(Thread.currentThread().getName() + " print " + count++);
}
}
}
public class ThreadDemo{
public static void main(String[] args) {
PrintThread printThread = new PrintThread();
Thread t1 = new Thread(printThread, "Thread-1");
Thread t2 = new Thread(printThread, "Thread-2");
Thread t3 = new Thread(printThread, "Thread-3");
Thread t4 = new Thread(printThread, "Thread-4");
Thread t5 = new Thread(printThread, "Thread-5");
Thread t6 = new Thread(printThread, "Thread-6");
Thread t7 = new Thread(printThread, "Thread-7");
Thread t8 = new Thread(printThread, "Thread-8");
Thread t9 = new Thread(printThread, "Thread-9");
Thread t10 = new Thread(printThread, "Thread-10");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
t7.start();
t8.start();
t9.start();
t10.start();
}
}
运行结果如下:
Thread-1 print 0
Thread-6 print 4
Thread-4 print 1
Thread-2 print 2
Thread-5 print 3
Thread-10 print 5
Thread-6 print 7
Thread-3 print 8
Thread-8 print 6
Thread-7 print 11
Thread-9 print 10
Thread-1 print 12
Thread-4 print 13
Thread-5 print 14
Thread-2 print 15
Thread-3 print 17
Thread-6 print 16
Thread-10 print 18
Thread-8 print 20
Thread-7 print 19
Thread-9 print 9
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java并发程序入门介绍 - Python技术站