⼋个经典的java多线程编程题⽬
⽂章⽬录
1、要求线程a执⾏完才开始线程b, 线程b执⾏完才开始线程
ample.javatest.theardTest.MultiThreadAlgorithm;
/**
* 要求线程a执⾏完才开始线程b, 线程b执⾏完才开始线程
*
* join()解释:blog.csdn/qq_18505715/article/details/79795728
*
* wait()  和  notify()  解释:blog.csdn/chaozhi_guo/article/details/50249177
*
* join()的作⽤:主要作⽤是同步,它可以使得线程之间的并⾏执⾏变为串⾏执⾏。在A线程中调⽤了B线程的join()⽅法时,表⽰只有当B线程执⾏完毕时,A线程才能继续执⾏。
*
* public void joinDemo(){
*    //....
*    Thread t=new Thread(payService);
*    t.start();
*    //....
*    //其他业务逻辑处理,不需要确定t线程是否执⾏完
*    insertData();
html不同浏览器布局全乱了*    //后续的处理,需要依赖t线程的执⾏结果,可以在这⾥调⽤join⽅法等待t线程执⾏结束
*    t.join();
* }
*
*/
public class T1T2T3 {
java经典上机编程题public static class PrintThread extends Thread{
PrintThread(String name){
super(name);
}
@Override
public void run(){
for(int i =0; i <100; i++){
System.out.println(getName()+" : "+ i);
}
}
}
public static void main(String[] args){
PrintThread t1 =new PrintThread("a");
PrintThread t2 =new PrintThread("b");
PrintThread t3 =new PrintThread("c");
try{
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
/**
* 我对于join⽅法的理解:
*
*    join() 的源码:
*    public final void join(long millis) throws InterruptedException {
*        synchronized(lock) {
*        ...
*
*          while (isAlive()) {
*              lock.wait(0);
*          }
*        ...
*        }
*    }
*
*    其实就是main()线程调⽤join()后,synchronized(lock)语句块,获得lock的锁,
*
*    然后判断如果t1线程isAlive(), 就⼀直lock.wait(), 让⾃⼰(main()线程)阻塞住,
*
*    直到t1线程 !isAlive 后才不wait, 等待着被notify(), 然后t1 die后会调⽤ifyAll()。
*
*
*    注意:这⾥lock.wait(0)虽然在t1.join()内,但是join()内的代码不是运⾏在t1线程中,⽽是运⾏在main()线程中, *          t1线程中运⾏的是其run()⽅法内的代码。
*
*/
2、两个线程轮流打印数字,⼀直到100
ample.javatest.theardTest.MultiThreadAlgorithm;
/**
* 两个线程轮流打印数字,⼀直到100
*
* Java的wait()、notify()学习:
* blog.csdn/boling_cavalry/article/details/77995069
*/
public class TakeTurnsPrint {
public static class TakeTurns {
private static boolean flag =true;
private static int count =0;
public synchronized void print1(){
for(int i =1; i <=50; i++){
while(!flag){
try{
System.out.println("print1: wait before");
wait();
System.out.println("print1: wait after");
}catch(InterruptedException e){
}
}
System.out.println("print1: "+++count);
flag =!flag;
notifyAll();
}
}
}
public synchronized void print2(){
for(int i =1; i <=50; i++){
while(flag){
try{
System.out.println("print2: wait before");
wait();
System.out.println("print2: wait after");
}catch(InterruptedException e){
}
}
System.out.println("print2: "+++count);
flag =!flag;
notifyAll();
}
}
}
public static void main(String[] args){
TakeTurns takeTurns =new TakeTurns();
new Thread(new Runnable(){
@Override
public void run(){
takeTurns.print1();
}
}).start();
new Thread(new Runnable(){
@Override
public void run(){
takeTurns.print2();
}
}).start();
}
python学起来难吗
}
3、写两个线程,⼀个线程打印1~ 52,另⼀个线程打印A~Z,打印顺序是12A34B…5152Z
ample.javatest.theardTest.MultiThreadAlgorithm;
/**
* 两线程,⼀个打印数字从1到52,另⼀个打印字母从A到Z,输出:5152Z
*/
public class TakeTurnsPrint2 {
private boolean flag;
private int count;
public synchronized void printNum(){
for(int i =0; i <26; i++){
while(flag){
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
flag =!flag;
System.out.print(++count);
System.out.print(++count);
notify();
}
}
public synchronized void printLetter(){
for(int i =0; i <26; i++){
while(!flag){
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
flag =!flag;
System.out.print((char)(65+ i));
notify();
}
}
public static void main(String[] args){
TakeTurnsPrint2 turnsPrint2 =new TakeTurnsPrint2();
new Thread(new Runnable(){
@Override
public void run(){
turnsPrint2.printNum();
}
}).start();
new Thread(new Runnable(){
@Override
public void run(){
turnsPrint2.printLetter();
}前后端分离怎么实现
}).start();
}
}
4、编写⼀个程序,启动三个线程,三个线程的ID分别是A,B,C;,每个线程将⾃⼰的ID值在屏幕上打印5遍,打印顺序是ABCABC…
ample.javatest.theardTest.MultiThreadAlgorithm;
ample.javatest.theardTest.MultiThreadAlgorithm;
/**
* 编写⼀个程序,启动三个线程,三个线程的ID分别是A,B,C;,每个线程将⾃⼰的ID值在屏幕上打印5遍,打印顺序是 */
public class ABCABCABC {
private int flag =0;
public synchronized void printA(){
for(int i =0; i <5; i++){
while(flag !=0){
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
flag =1;
System.out.print("A");
notifyAll();
}
}
java instancepublic synchronized void printB(){
for(int i =0; i <5; i++){
while(flag !=1){
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
flag =2;
System.out.print("B");
notifyAll();
}
}
public synchronized void printC(){
for(int i =0; i <5; i++){
while(flag !=2){
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
flag =0;
System.out.print("C");
notifyAll();spider品牌
}
}
public static void main(String[] args){
ABCABCABC abcabcabc =new ABCABCABC();
new Thread(new Runnable(){
@Override
public void run(){
abcabcabc.printA();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。