zoukankan      html  css  js  c++  java
  • JAVA线程操作常见面试题 包括不使用内部类对多个线程加减1

    class ManyThreads2 {
    private int j = 0;
    public synchronized void inc() {
    j++;
    System.out.println(Thread.currentThread().getName() + "inc" + j); }
    public synchronized void dec() {
    j--;
    System.out.println(Thread.currentThread().getName() + "dec" + j);
    }}
    public class MyTest extends Thread {
    private ManyThreads2 many = new ManyThreads2();
    public void run()
    { many.inc();
    many.dec();
    many.inc();
    many.dec();
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    MyTest myTest = new MyTest();
    myTest.start(); }
    }

    分别10次打印 输出A B C 


    public class MyTest extends Thread {
    char cha;
    private int id;
    int num=0;
    static int count=0;
    public MyTest(int id, char cha)
    { this.id=id;
    this.cha=cha;
    }

    public synchronized void run()
    { while(num<10){
    if(count%3==id){
    System.out.println(id);
    count++;
    num++;
    }
    } }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    new MyTest(1,'A').start();
    new MyTest(2,'B').start();
    new MyTest(3,'C').start(); }
    }

  • 相关阅读:
    C#异步编程:多线程基础Thread类
    WPF:TextBox控件禁用中文输入
    C#:泛型的协变和逆变
    C#:泛型接口
    C#:泛型委托
    C#:泛型类
    Jetbrains Rider:缺少.NET Framework 4.5.2
    C#:泛型方法
    C#:泛型
    C#:接口
  • 原文地址:https://www.cnblogs.com/zhouxiansheng/p/4342590.html
Copyright © 2011-2022 走看看