题目一:子线程循环2次,主线程循环2次,然后子线程循环2次,主线程循环2次,这样循环10次;
public class Demo2 {
static class Mywork {
private volatile boolean flag=true;//代表是否等待
private synchronized void sub(){
while (! flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 2; i++) {
System.out.println("sub run "+ i);
}
flag=false;
this.notify();
}
private synchronized void mainRun(){
while (flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 2; i++) {
System.out.println("main run----------- "+i);
}
flag=true;
this.notify();
}
}
public static void main(String[] args) {
Mywork mywork=new Mywork();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
mywork.sub();
}
}
}).start();
for (int i = 0; i < 10; i++) {
mywork.mainRun();
}
}
}
打印结果:
sub run 0
sub run 1
main run----------- 0
main run----------- 1
sub run 0
sub run 1
main run----------- 0
main run----------- 1
...
...此处省略
...
sub run 0
sub run 1
main run----------- 0
main run----------- 1
用显示锁的方式:
public class LockCondtionDemo {
private static class Mywork{
private volatile boolean flag=true;//是否等待
private Lock lock=new ReentrantLock();
private Condition condition=lock.newCondition();
public void sub(){
try{
lock.lock();
while (!flag){
try {
condition.await();//释放锁
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("sub run ----------------");
flag=false;
condition.signal();
}finally {
lock.unlock();//这里一定要释放锁
}
}
public void main(){
try{
lock.lock();
while(flag){
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("main run");
flag=true;
condition.signal();
}finally {
lock.unlock();
}
}
}
public static void main(String[] args) {
Mywork mywork=new Mywork();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
mywork.sub();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
mywork.main();
}
}
}).start();
}
}
题目二:写两个线程,一个线程打印152,另一个线程打印AZ,打印顺序是12A34B...5152Z;
public class Demo1 {
static class Behaver{
private volatile boolean flag=true;
private int count=1;
public synchronized void printNum(){
while(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(count*2-1);
System.out.print(count*2);
flag=false;
this.notify();
}
public synchronized void printChar(){
while(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print((char)(count+'A'-1));
count++;
flag = true;
this.notify();
}
}
public static void main(String[] args) {
Behaver behaver=new Behaver();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 26; i++) {
behaver.printNum();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 26; i++) {
behaver.printChar();
}
}
}).start();
}
}
打印结果:
12A34B56C78D910E1112F1314G1516H1718I1920J2122K2324L2526M2728N2930O3132P3334Q3536R3738S3940T4142U4344V4546W4748X4950Y5152Z
网友评论