线程写法1 无法返回结果
package test3;//无法返回结果
public class Demo3Thread {
public static void main(String[] args) {
Runnable target = new Runnable() {
/**
* 匿名内部类
*/
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("子线程1:" + i);
}
}
};
Thread t1 = new Thread(target);
t1.start();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
System.out.println("子线程2:"+i);
}
}).start();
Thread t = new mythread();
t.start();
for (int i = 0; i < 5; i++) {
System.out.println("主线程:"+i);
}
}
}
class mythread extends Thread{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("子线程:"+i);
}
}
}
线程写法二 可以返回结果
package test3;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* callabel
*/
public class Demo4Thread {
public static void main(String[] args) {
Callable<String> call= new mycallabel(100);
FutureTask<String> f1 = new FutureTask<>(call);
Thread t2 = new Thread(f1);
t2.start();
try {
String rs1 = f1.get();
System.out.println(rs1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class mycallabel implements Callable<String>{
private int n;
public mycallabel(int n) {
this.n = n;
}
@Override
public String call() throws Exception {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += i;
}
return "子线程的结果为:"+sum;
}
}
线程安全问题---------》上锁
package Case;
/**
* 多线程安全 atm取钱
*/
public class Case6 {
public static void main(String[] args) {
atm acc = new atm("icbc",10000);
new drawmoney(acc,"ming").start();
new drawmoney(acc,"hong").start();
}
}
package Case;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class atm {
private String cardid;
private double money;
//2
private final Lock lock = new ReentrantLock();
public atm() {
}
public atm(String cardid, double money) {
this.cardid = cardid;
this.money = money;
}
public String getCardid() {
return cardid;
}
public void setCardid(String cardid) {
this.cardid = cardid;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public void drawmoney(double money) {
// synchronized (this) {
// String name =Thread.currentThread().getName();
// if(this.money>=money){
// System.out.println(name+"取钱成功,吐出"+money);
// this.money -= money;
// System.out.println("剩余"+this.money);
// }else {
// System.out.println(name+"余额不足");
// }
//2
String name = Thread.currentThread().getName();
lock.lock();
try {
if (this.money >= money) {
System.out.println(name + "取钱成功,吐出" + money);
this.money -= money;
System.out.println("剩余" + this.money);
} else {
System.out.println(name + "余额不足");
}
} finally {
lock.unlock();
}
}
}
运行结果
线程池
package test3;
import java.util.concurrent.*;
public class Demo5 {
public static void main(String[] args) {
/**
* public ThreadPoolExecutor(int corePoolSize,
* int maximumPoolSize,
* long keepAliveTime,
* TimeUnit unit,
* BlockingQueue<Runnable> workQueue,
* ThreadFactory threadFactory,
* RejectedExecutionHandler handler)
*/
ExecutorService pool = new ThreadPoolExecutor(3,5,6, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(5),Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
Runnable t =new mYRUNABLE();
pool.execute(t);
pool.execute(t);
pool.execute(t);
pool.execute(t);
pool.execute(t);
pool.execute(t);
}
}