Java多线程
创建线程
继承Thread类
package com.createThread;
import org.junit.Test;
public class TestThread1 extends Thread {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("打游戏");
}
}
@Test
public void testThread() {
Thread thread = new TestThread1();
thread.start();
for (int i = 0; i < 20; i++) {
System.out.println("敲代码");
}
}
}
实现Runnable接口
package com.createThread;
import org.junit.Test;
public class TestThread2 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("打游戏");
}
}
@Test
public void startThread() {
TestThread2 thread = new TestThread2();
new Thread(thread).start();
for (int i = 0; i < 20; i++) {
System.out.println("敲代码");
}
}
}
实现Callable接口
package com.createThread;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.Test;
public class TestThread3 implements Callable<Boolean> {
@Override
public Boolean call() throws Exception {
for (int i = 0; i < 20; i++) {
System.out.println("打游戏");
}
return true;
}
@Test
public void testThread() {
ExecutorService es = Executors.newFixedThreadPool(1);
es.submit(new TestThread3());
for (int i = 0; i < 20; i++) {
System.out.println("敲代码");
}
}
}
状态转换
sleep()方法
package com.state;
public class TestBlock implements Runnable{
@Override
public void run() {
for(int i=10;i>0;i--) {
System.out.println("倒计时:"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Thread(new TestBlock()).start();
}
}
yield()方法
package com.state;
public class TestYield {
public static void main(String[] args) {
new Thread(()->{
System.out.println("Thread1 start");
Thread.yield();
System.out.println("Thread1 end");
}).start();
new Thread(()->{
System.out.println("Thread2 start");
System.out.println("Thread2 end");
}).start();
}
}
join()方法
package com.state;
public class TestJoin {
public static void main(String[] args) {
new Thread(()->{
System.out.println("Thread1 start");
System.out.println("wait Thread2 finish");
Thread t2 = new Thread(()->{
System.out.println("Thread2 start");
System.out.println("Thread2 end");
});
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread1 end");
}).start();
}
}
线程安全
synchronized
package com.synchronize;
public class TestSynchronized {
public static void main(String[] args) {
Account account = new Account(100);
new Thread(new TakeMoney(account, 10)).start();
new Thread(new TakeMoney(account, 20)).start();
new Thread(new TakeMoney(account, 30)).start();
new Thread(new TakeMoney(account, 40)).start();
new Thread(new TakeMoney(account, 50)).start();
}
}
class TakeMoney implements Runnable {
private Account a;
private int money;
public TakeMoney(Account a,int money) {
this.a = a;
this.money = money;
}
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
a.getOutMoney2(this.money);
}
}
class Account {
private int money;
public Account(int money) {
this.money = money;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public synchronized void getOutMoney(int money) {
if(this.money < money) {
System.out.println("余额不足,无法取出");
}else {
this.money -= money;
System.out.println("你已取出"+money+"元,账户余额为"+this.money+"元");
}
}
public void getOutMoney2(int money) {
synchronized (this) {
if(this.money < money) {
System.out.println("余额不足,无法取出");
}else {
this.money -= money;
System.out.println("你已取出"+money+"元,账户余额为"+this.money+"元");
}
}
}
}
CopyOnWriteArrayList
package com.synchronize;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class TestList {
public void testList1() {
List<String> list = new ArrayList<String>();
for(int i=0;i<10000;i++) {
new Thread(()->{
list.add("aa");
}).start();
}
System.out.println(list.size());
}
public void testList2() {
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
for(int i=0;i<10000;i++) {
new Thread(()->{
list.add("aa");
}).start();
}
System.out.println(list.size());
}
public static void main(String[] args) {
TestList test = new TestList();
test.testList1();
test.testList2();
}
}
其他
生产者消费者问题
package com.productor_consumer;
public class ProductorConsumer1 {
public static void main(String[] args) {
SynContainer container = new SynContainer();
new Productor(container).start();
new Consumer(container).start();
}
}
class Productor extends Thread{
SynContainer container;
public Productor(SynContainer container) {
this.container = container;
}
@Override
public void run() {
for(int i=0;i<100;i++) {
System.out.println("生产第"+i+"个面包");
container.push(new Bread(i));
}
}
}
class Consumer extends Thread{
SynContainer container;
public Consumer(SynContainer container) {
this.container = container;
}
@Override
public void run() {
for(int i=0;i<100;i++) {
System.out.println("消费第"+container.pop().id+"个面包");;
}
}
}
class SynContainer{
Bread[] bread = new Bread[10];
int count = 0;
public synchronized void push(Bread b) {
while(count == bread.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
bread[count] = b;
count++;
this.notifyAll();
}
public synchronized Bread pop() {
while(count == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
Bread b = bread[count];
this.notifyAll();
return b;
}
}
class Bread{
int id;
public Bread(int id) {
this.id = id;
}
}
内存可见
package com.testvolatile;
public class TestVolatile {
public volatile static int num = 0;
public static void main(String[] args) {
new Thread(()->{
while(num == 0) {
}
}).start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
num = 1;
}
}
并发下的单例模式
package com.danli;
public class Danli {
private volatile static Danli instance;
private Danli() {
}
public static Danli getInstance() {
if(instance != null) {
return instance;
}
synchronized (Danli.class) {
if(instance == null) {
instance = new Danli();
}
}
return instance;
}
}
定时调度
package com.time;
import java.util.Timer;
import java.util.TimerTask;
public class TestTimer {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new MyTimeTask(), 1000);
timer.schedule(new MyTimeTask(), 1000,2000);
}
}
class MyTimeTask extends TimerTask{
@Override
public void run() {
for(int i=0;i<10;i++) {
System.out.println("任务调度一次");
}
System.out.println("end------------");
}
}