多线程例题1
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class ThreadDemo15 {
public static void main(String[] args) {
int[] arrs=new int[1000000];
Random random=new Random();
for (int i = 0; i <arrs.length ; i++) {
arrs[i]=(random.nextInt(100)+i);
}
FutureTask<Integer>task1=new FutureTask<Integer>(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int temp=0;
for (int i = 0; i <arrs.length ; i=i+2) {
temp +=arrs[i];
}
System.out.println("线程1:"+temp);
return temp;
}
});
Thread t1=new Thread(task1);
t1.start();/art
FutureTask<Integer> task2=new FutureTask<>(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int temp=0;
for (int i = 0; i <arrs.length ; i++) {
temp+=arrs[i];
}
return temp;
}
});
Thread t2=new Thread(task2);
t2.start();
}
}
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ThreadDemo15 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
int[] arrs=new int[1000000];
Random random=new Random();
for (int i = 0; i <arrs.length ; i++) {
arrs[i]=(random.nextInt(100)+i);
}
FutureTask<Integer>task1=new FutureTask<Integer>(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int temp=0;
for (int i = 0; i <arrs.length ; i=i+2) {
temp +=arrs[i];
}
System.out.println("线程1:"+temp);
return temp;
}
});
Thread t1=new Thread(task1);
t1.start();/art
FutureTask<Integer> task2=new FutureTask<>(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int temp=0;
for (int i = 0; i <arrs.length ; i++) {
temp+=arrs[i];
}
System.out.println("线程2:"+temp);
return temp;
}
});
Thread t2=new Thread(task2);
t2.start();
int sum=task1.get()+task2.get();
System.out.println("最终结果"+sum);
//t1.join();
//t2.join();
}
}
在这里插入代码片
int[] arrs=new int[10000000];
int[] arrs=new int[2];
57+152=208
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ThreadDemo15 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
long stime=System.currentTimeMillis();
//int[] arrs=new int[10000000];
int[] arrs=new int[2];
Random random=new Random();
for (int i = 0; i <arrs.length ; i++) {
int temp=random.nextInt(100)+1;
arrs[i]=temp;
//arrs[i]=(random.nextInt(100)+i);
System.out.println(temp);
}
FutureTask<Integer>task1=new FutureTask<Integer>(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int temp=0;
for (int i = 0; i <arrs.length ; i=i+2) {
temp +=arrs[i];
}
System.out.println("线程1:"+temp);
return temp;
}
});
Thread t1=new Thread(task1);
t1.start();/art
FutureTask<Integer> task2=new FutureTask<>(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int temp=0;
for (int i = 0; i <arrs.length ; i++) {
temp+=arrs[i];
}
System.out.println("线程2:"+temp);
return temp;
}
});
Thread t2=new Thread(task2);
t2.start();
int sum=task1.get()+task2.get();
System.out.println("最终结果"+sum);
long etime=System.currentTimeMillis();
System.out.println("最终执行时间"+(etime-stime));
//t1.join();
17 18
有点没理解 题目再看一下
偶数下标元素的和 奇数下标元素的和 是不是不太对?
复习
![在这里插入图片描述](https://img-blog.csdnimg.cn/71bdd72587994dbc89e405aa4121bc4b.png
上题回顾 多线程
代码含义与我所表达意思不符 for循环内 改变相应条件进行
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ThreadDemo15 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
long stime=System.currentTimeMillis();
//int[] arrs=new int[10000000];
int[] arrs=new int[2];
Random random=new Random();
for (int i = 0; i <arrs.length ; i++) {
int temp=random.nextInt(100)+1;
arrs[i]=temp;
//arrs[i]=(random.nextInt(100)+i);
System.out.println(temp);
}
FutureTask<Integer>task1=new FutureTask<Integer>(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int temp=0;
for (int i = 1; i <arrs.length ; i=i+2) {
temp +=arrs[i];
}
System.out.println("线程1:"+temp);
return temp;
}
});
Thread t1=new Thread(task1);
t1.start();/art
FutureTask<Integer> task2=new FutureTask<>(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int temp=0;
for (int i = 0; i <arrs.length ; i=i+2) {
temp+=arrs[i];
}
System.out.println("线程2:"+temp);
return temp;
}
});
Thread t2=new Thread(task2);
t2.start();
int sum=task1.get()+task2.get();
System.out.println("最终结果"+sum);
long etime=System.currentTimeMillis();
System.out.println("最终执行时间"+(etime-stime));
//t1.join();
//t2.join();
}
}
int[] arrs=new int[4];
public class ThreadSleep {
public static void main(String[] args) throws InterruptedException {
Thread thread=new Thread(()->{
try{
Thread.sleep(60*60*1000);
}catch (InterruptedException e){
System.out.println("我接收到了中止执行的通知");}
});thread.start();
Thread.sleep(1000);
System.out.println("终止子线程 thread");
thread.interrupt();
}
}
##ThreadSleep
### 3
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
public class ThreadTimeUtil {
public static void main(String[] args) throws InterruptedException {
System.out.println("主线程开始执行了"+ LocalDateTime.now());
TimeUnit.DAYS.sleep(1);
Thread.sleep(24*60*60*1000);
}
在这里插入代码片
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
public class ThreadTimeUtil {
public static void main(String[] args) throws InterruptedException {
System.out.println("主线程开始执行了"+ LocalDateTime.now());
TimeUnit.SECONDS.sleep(1);
Thread.sleep(24*60*60*1000);
System.out.println("主线程又开始执行了"+LocalDateTime.now());
}
}
定义为秒级 加速sleep
TimeUnit.SECONDS.sleep(3);
//Thread.sleep(24*60*60*1000);
总结
线程状态
*### 三级标题
四级标题
五级标题
六级标题*
public class ThreadState {
public static void main(String[] args) {
printState();
}
private static void printState(){
for (Thread.State item:Thread.State.values()) {
System.out.println(item);
}
}
}
*##*重点记录
import java.util.concurrent.TimeUnit;
public class ThreadState {
public static void main(String[] args) throws InterruptedException {
// printState();
Thread t1=new Thread(()->{
System.out.println("当前线程状态2:"+Thread.currentThread().getState());
try{
TimeUnit.SECONDS.sleep(3);
}catch (InterruptedException e)
{
e.printStackTrace();
}
});
System.out.println("当前线程的状态:"+t1);
t1.start();
Thread.sleep(1000);
System.out.println("当前线程的状态3"+t1.getState());
t1.join();
System.out.println("当前线程的状态4:"+t1.getState());
}
private static void printState(){
for (Thread.State item:Thread.State.values()) {
System.out.println(item);
}
}
}
5为优先级
main为父类线程
System.out.println("当前线程的状态:"+t1.getState());
## 线程安全
public class ThreadDemo16 {
static class Counter{
private int number = 0;
private int MAX_COUNT = 0;
public Counter(int MAX_COUNT) {
this.MAX_COUNT = MAX_COUNT;
}
public void iner() {
for (int i = 0; i < MAX_COUNT; i++) {
number++;
}
}
public void decr() {
for (int i = 0; i < MAX_COUNT; i++) {
number--;
}
}
public int getNumber() {
return number;
}
public static void main(String[] args) {
Counter counter=new Counter(100000) ;
counter.iner();
counter.decr();
System.out.println("最终结果:"+counter.getNumber());
}}}
单线程
public static void main(String[] args) throws InterruptedException {
Counter counter=new Counter(100000) ;
Thread t1=new Thread(()->{
counter.iner();
});
Thread t2=new Thread(()->{
counter.decr();
});
//启动多线程
t1.start();
t2.start();
t1.join();
t2.join();
//counter.iner();
//counter.decr();
System.out.println("最终结果:"+counter.getNumber());
}
两次结果不一致
多线陈处理操作
多线程、
在这里插入代码片
Counter counter=new Counter(100000) ;
Thread t1=new Thread(()->{
counter.iner();
});
t1.start();
t1.join();
Thread t2=new Thread(()->{
counter.decr();
});
//启动多线程
//t1.start();
t2.start();
//t1.join();
t2.join();
//counter.iner();
//counter.decr();
System.out.println("最终结果:"+counter.getNumber());
}
抢占式执行所带来的线程安全问题
、狼多肉少
多个线程修改同一个变量
static class Counter{
private int number = 0;
private int MAX_COUNT = 0;
public Counter(int MAX_COUNT) {
this.MAX_COUNT = MAX_COUNT;
}
public int iner() {
int temp=0;
for (int i = 0; i < MAX_COUNT; i++) {
temp++;
}return temp;
}
public int decr() {
int temp=0;
for (int i = 0; i < MAX_COUNT; i++) {
temp--;
}return temp;
}
public int getNumber() {
return number;
}
static int num1=0;
static int num2=0;
public static void main(String[] args) throws InterruptedException {
Counter counter=new Counter(100000) ;
Thread t1=new Thread(()->{
num1=counter.iner();
});
//t1.start();
//t1.join();
Thread t2=new Thread(()->{
num2=counter.decr();
});
//启动多线程
//t1.start();
t1.start();
t2.start();
//t1.join();
t1.join();
t2.join();
//counter.iner();
//counter.decr();
System.out.println("最终结果:"+(num1+num2));
}}
原子性 一组操作封装成一个单元
原子性安全性解释
然后他不知道
把1又给赋景区了
最后结果又变为1了 本来应该是0
上图右为原子性操作解释
上上图为内存解释
内存可见性问题 多线程 导致结果出错