/*声明自己的一个线程类*/
public class Test_thread extends Thread {
//重写Thread类中的run方法
public void run(){
System.out.println("i am the thread");
}
}
public class Test_Class {
public static void main(String[] args){
Test_thread thread = new Test_thread();
thread.start();
}
}
public class Test_thread implements Runnable {
public void run(){
System.out.println("i am the thread");
}
}
public class Test_Class {
public static void main(String[] args){
Test_thread thread = new Test_thread();
thread.start();//编译错误
}
}
public class Test_Class {
public static void main(String[] args){
Thread thread = new Thread(new Test_thread());
thread.start();
}
}
private long tid; private volatile char name[]; public static native Thread currentThread() private boolean daemon = false; private volatile int threadStatus = 0; public final static int NORM_PRIORITY = 5;
public class Test_thread extends Thread{
public static int count = 0;
public void run(){
try {
Thread.sleep((int) (Math.random() * 100));
}catch(InterruptedException e){
}
count++;
}
}
public class Test_Class {
public static void main(String[] args){
Test_thread[] thread = new Test_thread[1000];
for(int a=0;a<1000;a++){
thread[a] = new Test_thread();
thread[a].start();
}
for(int a=0;a<1000;a++){
try {
thread[a].join();
}catch (InterruptedException e){
}
}
System.out.println(Test_thread.count);
}
}
public class Test_thread extends Thread{
public static int count = 0;
public void run(){
try {
Thread.sleep((int) (Math.random() * 100));
}catch(InterruptedException e){
}
/*使用关键字*/
synchronized (Test_thread.class){
count++;
}
}
}
public class Counter{
private int count;
/*为实例方法加此关键字*/
public synchronized int getCount(){
return count;
}
}
public class Counter{
private int count;
/*为实例方法加此关键字*/
synchronized(this){
return count;
}
}
public class Counter{
private static int count;
public static synchronized int getCount(){
return count;
}
}
/*实际上给这个类加上锁*/
public class Counter{
private int count;
synchronized(Counter.class){
return count;
}
}
public class Counter{
private int count;
public synchronized int getCount(){
return count;
}
}
/*每次获得该对象的锁之后,去获取最新的count数值*/
public class Counter{
/*使用关键字volatile*/
private volatile int count;
public int getCount(){
return count;
}
}
/*此关键字保证每次使用count的时候数据都是最新的*/
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有