「java多线程编程练习」多线程编程 java

博主:adminadmin 2022-12-13 07:24:08 106

今天给各位分享java多线程编程练习的知识,其中也会对多线程编程 java进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java多线程编程题之连续打印abc的几种解法

package com.demo.test;/**

* 基于两个lock实现连续打印abcabc....

* @author lixiaoxi

* */public class TwoLockPrinter implements Runnable { // 打印次数

private static final int PRINT_COUNT = 10; // 前一个线程的打印锁

private final Object fontLock; // 本线程的打印锁

private final Object thisLock; // 打印字符

private final char printChar; public TwoLockPrinter(Object fontLock, Object thisLock, char printChar) { this.fontLock = fontLock; this.thisLock = thisLock; this.printChar = printChar;

}

@Override public void run() { // 连续打印PRINT_COUNT次

for (int i = 0; i PRINT_COUNT; i++) { // 获取前一个线程的打印锁

synchronized (fontLock) { // 获取本线程的打印锁

synchronized (thisLock) { //打印字符 System.out.print(printChar); // 通过本线程的打印锁唤醒后面的线程

// notify和notifyall均可,因为同一时刻只有一个线程在等待 thisLock.notify();

} // 不是最后一次则通过fontLock等待被唤醒 // 必须要加判断,不然虽然能够打印10次,但10次后就会直接死锁

if(i PRINT_COUNT - 1){ try { // 通过fontLock等待被唤醒 fontLock.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

} public static void main(String[] args) throws InterruptedException { // 打印A线程的锁

Object lockA = new Object(); // 打印B线程的锁

Object lockB = new Object(); // 打印C线程的锁

Object lockC = new Object();

// 打印a的线程

Thread threadA = new Thread(new TwoLockPrinter(lockC, lockA, 'A')); // 打印b的线程

Thread threadB = new Thread(new TwoLockPrinter(lockA, lockB, 'B')); // 打印c的线程

Thread threadC = new Thread(new TwoLockPrinter(lockB, lockC, 'C')); // 依次开启a b c线程 threadA.start();

Thread.sleep(100); // 确保按顺序A、B、C执行 threadB.start();

Thread.sleep(100);

threadC.start();

Thread.sleep(100);

}

}

java多线程编程题 跪求求解

package com.zhidao20161220;

public class Main {

    private static int count=0;

    public static void main(String[] args) {

        // TODO Auto-generated method stub

      for(int i=0;i10;i++)

      {

          String name = "人员"+(i+1);

          ManThread manThread = new ManThread(name);

          manThread.start();

      }

    }

    

    public static synchronized void go(String name)

    {

        count++;

        System.out.println("线程"+Thread.currentThread().getName()+"执行,"+name+"正在通过山洞");

        try {

        if(count=10)

        {

            System.out.println("全员通过程序结束");

            

        }

            Thread.sleep(5000);

        } catch (InterruptedException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

package com.zhidao20161220;

public class ManThread extends Thread {

    private String name;

    public ManThread(String name)

    {

        this.name = name;

    }

    public void run() {

        Main.go(name);

    }

}

Java关于io和多线程的编程题,求代码

package know;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

public class T20 {

 public static void main(String[] args) throws IOException {

  Scanner s=new Scanner(System.in);

  String username=null;

  String password=null;

  System.out.println("输入用户名:");

  while(true){

   if(username==null){

    username=s.next();

    System.out.println("输入密码:");

   }else{

    password=s.next();

   }

   if(password!=null){

    s.close();

    break;

   }

  }

  

  BufferedReader br=null;

  MapString, String map=new HashMapString, String();

  try{

   br=new BufferedReader(new InputStreamReader(new FileInputStream("d:/test.txt")));

   String temp=null;   

   while((temp=br.readLine())!=null){

    String[] ss=temp.split("=");

    map.put(ss[0], ss[1]);

   }

  }catch(IOException e){

   throw e;

  }finally{

   if(br!=null)

    br.close();

  }

  String u=map.get("userName");

  String p=map.get("password");

  if(u.equals(username)p.equals(password)){

   System.out.println("登录成功");

  }else{

   System.out.println("用户名或密码错误");

  }

 }

}

package know;

public class T21 {

 public static void main(String[] args) throws InterruptedException {

  String[] persons=new String[]{"甲","乙","丙","丁","午","己","庚","辛","壬","癸"};

  for(int i=0;ipersons.length;i++){

   System.out.println(persons[i]+"正在过山洞");

   Thread.sleep(5000);

  }

 }

}

最后一个百度搜一个就行了,肯定比我画的好

基础Java题 试编写一个多线程的程序:启动4个线程。其中两个循环10次,每次将某全局变量加1,另两个循环1

public class Day18_A {

public static void main(String[] args) throws InterruptedException {

Recoun rec = Recoun.getRec();

Thread[] trr = new Thread[4];

for (int i = 0; i  4; i++) {

trr[i] = new Thread(new NumberTest(rec, i), "线程" + (i + 1) + ":\t");

}

for (Thread thread : trr) {

thread.start();

}

for (Thread thread : trr) {

thread.join();

}

System.out.println("所有线程结束查看结果:" + rec.getCount());

}

}

class NumberTest implements Runnable {

private Recoun re;

private int n;

NumberTest(Recoun r, int i) {

this.re = r;

this.n = i;

}

public void run() {

for (int i = 0; i  10; i++) {

re.method(n);

}

}

}

class Recoun {

private int count = 0;

private Recoun() {

}

private static final Recoun rec = new Recoun();

public static Recoun getRec() {

return rec;

}

public synchronized void method(int i) {

if (i % 2 == 0) {

System.out.println(Thread.currentThread().getName() + (count++));

} else {

System.out.println(Thread.currentThread().getName() + (count--));

}

}

public synchronized int getCount() {

return count;

}

}

java题目 编程题目 多线程

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();

}

}

JAVA多线程 编程题两个案例,不会写,求大神写出代码,万分感谢,多线程还没学。

/*

class A extends Thread

{

public void run()

{

try

{

Thread.sleep(1000);

}catch(Exception e)

{

System.out.println("A ERROR!");

}

System.out.println("AAAA");

}

}

class B extends Thread

{

public void run()

{

try

{

Thread.sleep(2000);

}catch(Exception e)

{

System.out.println("B ERROR!");

}

System.out.println("BBBB");

}

}

class C extends Thread

{

public void run()

{

try

{

Thread.sleep(3000);

}catch(Exception e)

{

System.out.println("C ERROR!");

}

System.out.println("CCCC");

}

}

public class Test_1

{

public static void main(String[] args)

{

A a = new A();

B b = new B();

C c = new C();

a.start();

b.start();

c.start();

}

}*/

public class Test_1

{

public static void main(String[] args)

{

A a = new A();

B b = new B();

C c = new C();

Thread ta = new Thread(a);

Thread tb = new Thread(b);

Thread tc = new Thread(c);

ta.start();

tb.start();

tc.start();

}

}

class A implements Runnable

{

public void run()

{

try

{

Thread.sleep(1000);

}catch(Exception e)

{

System.out.println("A ERROR!");

}

System.out.println("AAAA");

}

}

class B implements Runnable

{

public void run()

{

try

{

Thread.sleep(2000);

}catch(Exception e)

{

System.out.println("B ERROR!");

}

System.out.println("BBBB");

}

}

class C implements Runnable

{

public void run()

{

try

{

Thread.sleep(3000);

}catch(Exception e)

{

System.out.println("C ERROR!");

}

System.out.println("CCCC");

}

}

案例一的两种方法已经写好;现在有事,晚上再把案例二代码写一下,应该没关系吧!

抱歉,是一个线程类,我看错了,晚上重发一下代码!

java多线程编程练习的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于多线程编程 java、java多线程编程练习的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-13,除非注明,否则均为首码项目网原创文章,转载请注明出处。