1、写两个线程,一个线程打印数字,一个线程打印字母,且交替打印,打结果为:A12B34C56D78E910F1112G1314H1516I1718J1920K2122L2324M2526N2728O2930P3132Q3334R3536S3738T3940U4142V4344W4546X4748Y4950Z5152
代码如下:
锁控制实体类Suo:
public class Suo {
public boolean charIsDo=false;//主要用于先执行线程1,先打字母
public boolean isCharIsDo() {
return charIsDo;
}
public void setCharIsDo(boolean charIsDo) {
this.charIsDo = charIsDo;
}
}
线程1:
public class ThreadTest1 implements Runnable{
private Suo suo;
public int currentIndex=0;
public ThreadTest1(){
}
public ThreadTest1(Suo suo){
this.suo=suo;
}
@Override
public void run() {
synchronized(suo) {
while(true){
if(currentIndex>=26){
suo.notify();//中断本线程前要通知其它线
break;//中断,退出此线程
}
System.out.print((char)(currentIndex+65));
currentIndex++;
suo.setCharIsDo(true);
try {
suo.notify();
suo.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
线程2:
public class ThreadTest2 implements Runnable{
private Suo suo;
public int currentIndex=0;
public ThreadTest2(){
}
public ThreadTest2(Suo suo){
this.suo=suo;
}
public void run() {
synchronized(suo) {
while(true){
if(!suo.isCharIsDo()){//优先打字母
try {
suo.notify();
suo.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(currentIndex>=52){
suo.notify();//中断本线程前要通知其它线程
break;//中断,退出此线程
}
currentIndex++;
System.out.print(currentIndex);
currentIndex++;
System.out.print(currentIndex);
try {
suo.notify();
suo.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
测试Main方法代码:
public class Main {
public static void main(String[] args) {
Suo suo = new Suo();//锁
ThreadTest1 test1=new ThreadTest1(suo);
ThreadTest2 test2=new ThreadTest2(suo);
Thread thead1=new Thread(test1);
thead1.start();
Thread thead2=new Thread(test2);
thead2.start();
}
private int cur=0;
public void run() {
this.printTest();
}
Object lock=new Object();//临时变量锁
public void printTest(){
synchronized (lock) {
while(true){
if(cur>20){
lock.notify();
break;
}
Thread currentThread=Thread.currentThread();
System.out.println(currentThread.getName()+":"+cur++);
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
测试代码:
public class Main {
public static void main(String args[]){
ThreadTest thread1 = new ThreadTest();
Thread t1= new Thread(thread1);
Thread t2= new Thread(thread1);
t1.start();
t2.start();
}
}
代码如下:
锁控制实体类Suo:
public class Suo {
public boolean charIsDo=false;//主要用于先执行线程1,先打字母
public boolean isCharIsDo() {
return charIsDo;
}
public void setCharIsDo(boolean charIsDo) {
this.charIsDo = charIsDo;
}
}
线程1:
public class ThreadTest1 implements Runnable{
private Suo suo;
public int currentIndex=0;
public ThreadTest1(){
}
public ThreadTest1(Suo suo){
this.suo=suo;
}
@Override
public void run() {
synchronized(suo) {
while(true){
if(currentIndex>=26){
suo.notify();//中断本线程前要通知其它线
break;//中断,退出此线程
}
System.out.print((char)(currentIndex+65));
currentIndex++;
suo.setCharIsDo(true);
try {
suo.notify();
suo.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
线程2:
public class ThreadTest2 implements Runnable{
private Suo suo;
public int currentIndex=0;
public ThreadTest2(){
}
public ThreadTest2(Suo suo){
this.suo=suo;
}
public void run() {
synchronized(suo) {
while(true){
if(!suo.isCharIsDo()){//优先打字母
try {
suo.notify();
suo.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(currentIndex>=52){
suo.notify();//中断本线程前要通知其它线程
break;//中断,退出此线程
}
currentIndex++;
System.out.print(currentIndex);
currentIndex++;
System.out.print(currentIndex);
try {
suo.notify();
suo.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
测试Main方法代码:
public class Main {
public static void main(String[] args) {
Suo suo = new Suo();//锁
ThreadTest1 test1=new ThreadTest1(suo);
ThreadTest2 test2=new ThreadTest2(suo);
Thread thead1=new Thread(test1);
thead1.start();
Thread thead2=new Thread(test2);
thead2.start();
}
}
方法也要先获得这个对象的锁,所以这种方法比较霸道,我们不建议这么做,所以出现了第二种方法,添加临时对象锁,就是针对每个synchronized方法,都有一个临时对象Object,如下面的代码
线程代码
public class ThreadTest implements Runnable {private int cur=0;
public void run() {
this.printTest();
}
Object lock=new Object();//临时变量锁
public void printTest(){
synchronized (lock) {
while(true){
if(cur>20){
lock.notify();
break;
}
Thread currentThread=Thread.currentThread();
System.out.println(currentThread.getName()+":"+cur++);
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
测试代码:
public class Main {
public static void main(String args[]){
ThreadTest thread1 = new ThreadTest();
Thread t1= new Thread(thread1);
Thread t2= new Thread(thread1);
t1.start();
t2.start();
}
}