java多线程练习题

Python014

java多线程练习题,第1张

public class Test{

public static Object obj = new Object()

public static void main(String[] args){

new A().start()

new B().start()

}

}

class A extends Thread{

public void run(){

try{

synchronized(Test.obj){

for(int i = 1  i < 31i += 6){

Test.obj.notify()

System.out.println("线程A:"+ i)

System.out.println("线程A:"+ (i+1))

System.out.println("线程A:"+ (i+2))

Test.obj.wait()

}

}

}catch(Exception e){

e.printStackTrace()

}

}

}

class B extends Thread{

public void run(){

try{

synchronized(Test.obj){

for(int i = 4  i < 31i += 6){

Test.obj.notify()

System.out.println("线程B:"+ i)

System.out.println("线程B:"+ (i+1))

System.out.println("线程B:"+ (i+2))

Test.obj.wait()

}

}

}catch(Exception e){

e.printStackTrace()

}

}

}

public class DoubleThread {

public static void main(String[] args) {

Thread t1 = new Thread() {

@Override

public void run() {

for (char i = 'a'i <= 'z'i++) {

System.out.println(i)

}

}

}

Thread t2 = new Thread() {

@Override

public void run() {

for (char i = 'A'i <= 'Z'i++) {

System.out.println(i)

}

}

}

t1.start()

t2.start()

}

}