Java 多线程基础
Posted 11biscuits
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 多线程基础相关的知识,希望对你有一定的参考价值。
整体内容
Main.java
package 多线程;
/*
Contents:
1、Key terms and concepts
2、Working with threads
3、Concurrency issues
4、Synchronization
5、Volatile fields
6、Atomic objects
*/
public class Main {
public static void main(String[]argv){
//获得当前线程活动数量
System.out.println(Thread.activeCount());
//2 当前主线程与垃圾回收线程
//获得处理器可以运行最高线程数量
System.out.println(Runtime.getRuntime().availableProcessors());
//12 我的电脑6核一个核2个核心
System.out.println("Main Thread"+Thread.currentThread().getId());
//并发问题
/*
*解决策略
1 不共享资源,线程数据独立
2 使用不可变变量,例如String,当改变了String对象其实是获得新的String对象
3 使用同步 Synchronization
4 使用原子类(锁,避免死锁)
5 使用分解 partitioning
* */
}
}
创建线程
CreateThread.java 创建线程
package 多线程;
import java.util.*;
public class CreateThread{
public static void main(String[]argv){
/*创建线程的两种方法
*实现java.lang.Runnable接口
*继承java.lang.Thread,成为其派生类
**/
new Method_1().start();
(new Thread(new Method_2())).start();
System.out.println("Over");
}
}
class Method_1 extends Thread{
@Override
public void run(){
System.out.println("Method_1");
}
}
class Method_2 implements Runnable{
@Override
public void run(){
System.out.println("Method_2");
}
}
获得线程状态
gettingtThreadStats.java 获得线程状态
package 多线程;
public class gettingtThreadStats {
public static void main(String[]argv){
//线程状态
//新建状态->准备状态->运行状态->终结状态
//通过getState()方法可以获得线程的当前状态,返回值是一个状态的枚举值
Thread a=new Thread(new PalGo());
a.start();
System.out.println(a.getState());
System.out.println(a.getState());
System.out.println(a.getState());
System.out.println(a.getState());
System.out.println(a.getState());
try{
Thread.sleep(1000);
}catch(InterruptedException e){
}
System.out.println(a.getState());
}
}
class PalGo implements Runnable{
@Override
public void run(){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
}
}
}
Join方法
ThreadJoin.java Join方法
package 多线程;
public class ThreadJoin {
public static void main(String[]argv){
Thread a=new Thread(new Runnable(){
@Override
public void run(){
System.out.println("A");
}
});
Thread b=new Thread(new Runnable(){
@Override
public void run(){
try{
a.join();
}catch(InterruptedException e){
}
System.out.println("B");
}
});
b.start();
a.start();
/*一定会先输出A后输出B,因为当线程A结束后B才有可能输出
* */
}
}
线程中断信号
ThreadInterruptedDemo.java 线程中断信号
package 多线程;
public class ThreadInterruptedDemo {
public static void main(String[]argv){
Thread a=new Thread(new A());
a.start();
try{
Thread.sleep(5000);
}catch(InterruptedException e){
}
a.interrupt();//发出中断信号
}
static class A implements Runnable{
@Override
public void run(){
for(int i=0;i<9999999;i++){
if(Thread.currentThread().isInterrupted()){
return;
}
System.out.println(i);
}
}
}
}
竞争关系
CompetitionIssues.java 竞争关系
package 多线程;
import java.util.ArrayList;
import java.util.List;
//竞争关系
public class CompetitionIssues {
public static void main(String[]argv){
Data data=new Data();
data.data=1;
List<Thread> threads=new ArrayList<>();
for(int i=0;i<100000;i++){
Thread a=new Thread(new A(data));
a.start();
threads.add(a);
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(var thread:threads)
try{
thread.join();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.print(data.data);
}
static class Data{
public int data;
public void inc(){
data++;
}
}
static class A implements Runnable{
Data data;
A(Data data){
this.data=data;
}
@Override
public void run(){
data.inc();
}
}
}
可重入锁
ReentrantLockDemo.java 可重入锁
package 多线程;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.locks.*;
//可重入锁
public class ReentrantLockDemo {
public static void main(String[]argv){
Data data=new Data();
data.data=1;
List<Thread>threads=new ArrayList<>();
for(int i=0;i<100000;i++){
Thread a=new Thread(new A(data));
a.start();
threads.add(a);
}
for(var thread:threads)
try{
thread.join();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.print(data.data);
}
}
class Data{
public int data;
private Lock lock= new ReentrantLock();;
Data(){
}
public void inc(){
lock.lock();//当已经被锁时,在进行锁操作将会被阻塞
try{
data++;
}finally{
lock.unlock();
}
}
}
class A implements Runnable{
Data data;
A(Data data){
this.data=data;
}
@Override
public void run(){
data.inc();
}
}
线程同步
SynchronizedDemo.java 线程同步
package 多线程;
import java.util.ArrayList;
import java.util.List;
public class SynchronizedDemo {
public static void main(String[]argv){
Data1 data=new Data1();
data.data=1;
List<Thread> threads=new ArrayList<>();
for(int i=0;i<100000;i++){
Thread a=new Thread(new AA(data));
a.start();
threads.add(a);
}
for(var thread:threads)
try{
thread.join();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.print(data.data);
}
}
class Data1{
public int data;
public Object A;
Data1(){
A=new Object();
}
public void inc(){
synchronized(A){
data++;
}
}
/*synchronized(参数)
*参数为拿锁的地方,每个拿锁的地方只有一把锁,同步更新AA,即共同操作AA的同步锁
* */
/*
*public void inc(){
synchronized(this){
data++;
}
}
public synchronized void inc(){
data++;
}
*
* */
}
class AA implements Runnable{
Data1 data;
AA(Data1 data){
this.data=data;
}
@Override
public void run(){
data.inc();
}
}
volatile关键词
VolatileDemo.java volatile关键词
package 多线程;
/*添加volatile关键词的属性读取的时候将会直接从内存中读取,避免cache缓存导致的数据不一致*/
public class VolatileDemo {
public static void main(String[]argv){
VolatileDemoData data=new VolatileDemoData();
Thread a=new Thread(new VolatileA(data));
Thread b=new Thread(new VolatileB(data));
a.start();
b.start();
}
}
class VolatileDemoData{
public volatile int data;
VolatileDemoData(){
this.data=0;
}
}
class VolatileA implements Runnable{
VolatileDemoData data;
VolatileA(VolatileDemoData data){
this.data=data;
}
@Override
public void run(){
for(int i=0;i<100000;i++){
}
data.data=1;
System.out.println("A:A Over");
}
}
class VolatileB implements Runnable{
VolatileDemoData data;
VolatileB(VolatileDemoData data){
this.data=data;
}
@Override
public void run(){
while(data.data!=1){
System.out.println("B:A not Over");
}
}
}
wait与notify
WaitNotifyDemo.java wait与notify
package 多线程;
public class WaitNotifyDemo {
public static void main(String[]argv){
WaitNotifyDemoData data=new WaitNotifyDemoData();
Thread a=new Thread(new WaitNotifyDemoA(data));
Thread b=new Thread(new WaitNotifyDemoB(data));
a.start();
try{
Thread.sleep(5000);
}catch(InterruptedException e){
}
b.start();
try{
b.join();
a.join();
}catch(InterruptedException e){
}
}
}
class WaitNotifyDemoData{
public volatile int data;
WaitNotifyDemoData(){
this.data=0;
}
}
class WaitNotifyDemoA implements Runnable{
WaitNotifyDemoData data;
WaitNotifyDemoA(WaitNotifyDemoData data){
this.data=data;
}
@Override
public void run(){
synchronized(data){//data.wait()会自动释放锁,而sleep不会释放锁
System.out.println("A WAIT()");
try{
System.out.println("A wait");
data.wait();//等待被notify
System.out.println("notifyed");
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
class WaitNotifyDemoB implements Runnable{
WaitNotifyDemoData data;
WaitNotifyDemoB(WaitNotifyDemoData data){
this.data=data;
}
@Override
public void run(){
synchronized(data){
System.out.println("notify");
data以上是关于Java 多线程基础的主要内容,如果未能解决你的问题,请参考以下文章