. [问题描述]创建三个线程并启动,每个线程运行中每隔1秒钟更新一个共享变量var,让其加1,并打印出来。
[基本要求]
观察打印出来的顺序是不是按1递增的。如果不是,修改程序,使得打印的顺序是按1递增的。
[测试数据] 自定义。
这里有个卖票的例子可以符合这个要求, 加上线程锁就同步了!
//并发:两个或多个线程 同时访问一个共享资源的现象
public class Tickets implements Runnable {
int count = 100;// 总票数
public void run() {
while (true) {
// 1、给程序段加上互斥锁
synchronized (this) {
if (count < 1) {
System.out.println("票已售罄");
break;
}
count--;// 余票减1
System.out.println(Thread.currentThread().getName()
+ " 卖出了一张票,还剩" + count);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Tickets t = new Tickets();
Thread a = new Thread(t, "赣州站");
Thread b = new Thread(t, "广州站");
Thread c = new Thread(t, "上海站");
a.start();
b.start();
c.start();
}
}
定义线程类,实现Runnable接口。
import java.util.concurrent.atomic.AtomicInteger;
public class IntegerAutoAdd {
static AtomicInteger num = new AtomicInteger(0);
static class ThreadA implements Runnable {
@Override
public void run() {
int i = 0;
while (i < 5) {
num.incrementAndGet();
System.err.println("线程A第" + i + "次操作,num值为:" + num);
i++;
try {
Thread.currentThread();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
static class ThreadB implements Runnable {
@Override
public void run() {
int i = 0;
while (i < 5) {
num.incrementAndGet();
System.err.println("线程B第" + i + "次操作,num值为:" + num);
i++;
try {
Thread.currentThread();
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Thread a = new Thread(new ThreadA());
Thread b = new Thread(new ThreadB());
a.start();
b.start();
}
}
直接给这个共享变量加上volatile就好了
错误的代码
package com.xm.officialaccounts.utils;
import com.baomidou.mybatisplus.extension.api.R;
/**
* @author
* @date created in 10:00 2021/6/10
*/
public class testThread extends Thread {
static int var = 0;
@Override
public void run() {
while (true){
var++;
System.out.println(var);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
testThread a = new testThread();
testThread b = new testThread();
testThread c = new testThread();
a.start();
b.start();
c.start();
}
}
// 1
// 2
// 3
// 4
// 6
// 5
// 7
// 8
// 8
// 9
// 10
// 10
// 11
// 11
// 11
// 12
// 14
// 13
// 15
// 15
// 15
安全的
package com.xm.officialaccounts.utils;
/**
* @author
* @date created in 10:00 2021/6/10
*/
public class testThread extends Thread {
static volatile int var = 0;
public static synchronized void add(){
var++;
System.out.println("ThreadName:"+Thread.currentThread().getName()+" var:"+var);
}
@Override
public void run() {
while (true){
add();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
testThread a = new testThread();
testThread b = new testThread();
testThread c = new testThread();
a.start();
b.start();
c.start();
/* ThreadName:Thread-0 var:1
ThreadName:Thread-1 var:2
ThreadName:Thread-2 var:3
ThreadName:Thread-0 var:4
ThreadName:Thread-1 var:5
ThreadName:Thread-2 var:6
ThreadName:Thread-2 var:7
ThreadName:Thread-1 var:8
ThreadName:Thread-0 var:9
ThreadName:Thread-2 var:10
ThreadName:Thread-1 var:11
ThreadName:Thread-0 var:12
ThreadName:Thread-2 var:13
ThreadName:Thread-0 var:14
ThreadName:Thread-1 var:15
ThreadName:Thread-2 var:16
ThreadName:Thread-1 var:17
ThreadName:Thread-0 var:18
ThreadName:Thread-2 var:19
ThreadName:Thread-1 var:20
ThreadName:Thread-0 var:21*/
}
}