1.线程共享数据操作
题目1 员工发彩票,从前门和后门入场领取彩票,打印员工
入场入场顺序及领取的彩票信息
import java.util.ArrayList;
public class text {
public static void main (String[] args) {
Men run = new Men();
Thread t1 = new Thread(run,"前门" );
Thread t2 = new Thread(run,"后门" );
t1.start();
t2.start();
}
}
class Men implements Runnable{
private int pNum = 100 ;
private int qNum = 0 ;
private int hNum = 0 ;
@Override
public void run () {
while (true ) {
synchronized(this ) {
if (pNum <= 0 ) {
break ;
}
String name = Thread.currentThread().getName();
if (name.equals("前门" )) {
System.out .println("编号:" + (100 - pNum + 1 )+
"从" +name+"入场 号码是:" );
printcp();
pNum--;
qNum++;
}
if (name.equals("后门" )) {
System.out .println("编号:" + (100 - pNum + 1 )+
"从" +name+"入场 号码是:" );
printcp();
pNum--;
hNum++;
}
if (pNum == 0 ) {
System.out .println("从前门入场" + qNum);
System.out .println("从后面入场" + hNum);
}
}
Thread.yield ();
}
}
public void printcp () {
ArrayList<Integer> list = new ArrayList<>();
while (list.size() < 7 ) {
int num = (int )(Math.random()* (30 -1 +1 )+1 );
if (!list.contains(num)) {
list.add(num);
}
}
System.out .println(list);
}
}
2.停止线程
1.只要线程停止了就叫停止线程
2.测试interrupt中断线程
public class text {
public static void main(String[] args) {
Stop stop = new Stop();
Thread thread = new Thread(stop);
thread.start();
thread.interrupt();
System.out.println("线程中断了" );
try {
Thread.sleep(1000 );
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程结束" );
}
}
class Stop implements Runnable {
@Override
public void run() {
int i = 0 ;
while (!Thread.currentThread().isInterrupted()) {
long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start < 1000 ) {
}
System.out.println(Thread.currentThread().getName() + "-" + i++);
}
}
}
测试结果:
1 .不能测子线程
2 .实际上这个interrupt()方法设置了一个布尔值 isInterrupted
3 .如果线程中有waiter()等待 或者sleep睡眠
这时调用interrupt方法就会抛出一个异常 并且清除中断状态
4 .如果线程中没有等待或者休眠 这时会调用interrupt方法会设置中断状态(true /false 的改变)
5 .interrupt方法尽量不要使用,如果停止线程,直接使用标记停止即可
6 .只有遇到等待状态时,可以使用该方法,强行清楚该等待状态
2.interrupt 方法的强行解冻
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class text {
public static void main(String[] args) {
InterruptRunnable interruptRunnable = new InterruptRunnable();
Thread thread1 = new Thread(interruptRunnable, "线程1" );
Thread thread2 = new Thread(interruptRunnable, "线程2" );
thread1.start();
thread2.start();
for (int i = 0 ; i < 50 ; i++) {
if (i == 25 ) {
thread1.interrupt();
thread2.interrupt();
break ;
}
System.out.println(i + "*-*-*" );
}
System.out.println("主线程结束" );
}
}
class InterruptRunnable implements Runnable {
@Override
public synchronized void run() {
while (true ) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss" );
String format = dateFormat.format(date );
System.out.println(Thread.currentThread().getName() + format);
}
}
}
1. wait 方法是 Object 类的
2. 注意: wait 方法必须用锁对象去调用
3. 调用 interrupt 方法强行解决清除wait 等待状态
4. 注意: wait 和notify一般成对出现,都是使用锁对象调用
6. this.notify(); 随机唤醒等待线程中一个
7. notifyAll() 唤醒所有等待的线程