首页 > 代码库 > java线程同步

java线程同步

package com.shijiaoyun.model;

public class TestThread extends Thread {
 private String str;
 private static Object lock = new Object();

 
 public TestThread(String str){
  this.str = str;
 }
 
 public static void main(String[] args) {
  TestThread a = new TestThread("线程A:");
  TestThread b = new TestThread("线程B:");
  a.start();
  b.start();
 }

 @Override
 public  void run() {
  synchronized(lock){
   for(int i = 0; i < 5; i++){
    System.out.println(str + i);
   }
        }
 }

 public String getStr() {
  return str;
 }

 public void setStr(String str) {
  this.str = str;
 }
}

java线程同步