JAVA [ 多线程 ]

来源:互联网 发布:手机淘宝怎么快速秒杀 编辑:程序博客网 时间:2024/06/03 05:24
public class ThreadDemo1 {
public static void main(String[] args){
new TestThread().run();
while(true){
System.out.println("main(): " + Thread.currentThread().getName());
}
}
}
class TestThread{
public void run(){
while(true){
System.out.println("run(): " + Thread.currentThread().getName());
}
}
}
//单线程

//main中程序一直等待执行

public class ThreadDemo2 {
public static void main(String[] args){
// new Thread()创建线程
// start 启动线程

new TestThread2().start();
while(true){
System.out.println("main(): " + Thread.currentThread().getName());
}
}
}


class TestThread2 extends Thread{
public void run(){
while(true){
System.out.println("run(): " + Thread.currentThread().getName());
}
}
}
//多线程

public class ThreadDemo3 {
public static void main(String[] args){
// new Thread()创建线程
// start 启动线程

Thread tt = new TestThread2();
//后台线程当前台结束时随之一起结束
tt.setDaemon(true);
tt.start();
}
}

public class ThreadDemo4 {
public static void main(String[] args){
// new Thread()创建线程
// start 启动线程

Thread tt = new TestThread2();
tt.start();
int index=0;
while(true){
//主线程循环100遍,第一个线程合并到主线程中
//变成单线程,主线程等待子线程
//1秒后分开成为两个线程

if(index++ == 100)
try {
tt.join(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("main():" + Thread.currentThread().getName());
}
}
}

public class ThreadDemo5 {
public static void main(String[] args){
//4个线程对象各卖各的100张票
//new TestThread4().start();
//new TestThread4().start();
//new TestThread4().start();
//new TestThread4().start();



//1个对象,4个线程100张票
//事实上一个对象,一个线程在卖票
Thread tt = new TestThread4();
//tt.start();
//tt.start();
//tt.start();
//tt.start();

//创建资源对象,一个对象,多个线程处理
TestThread5 t = new TestThread5();
//new Thread(t).start();
//new Thread(t).start();
//new Thread(t).start();
//new Thread(t).start();

/**
* 意外连续打印相同的票号,cpu切换
* 需要保持原子性,代码好比独木桥,只能一个线程执行
*/
TestThread6 t6 = new TestThread6();
//new Thread(t6).start();
//new Thread(t6).start();
//new Thread(t6).start();
//new Thread(t6).start();

/**
* 同步,对象同步
* 对象有标志位,初始化为1,同步后为0,同步结束变为1,到synchronized检查
*/
TestThread7 t7 = new TestThread7();
//new Thread(t7).start();
//new Thread(t7).start();
//new Thread(t7).start();
//new Thread(t7).start();


/**
* 同步,方法同步
* 同步对象this
*/
TestThread8 t8 = new TestThread8();
//new Thread(t8).start();
//new Thread(t8).start();
//new Thread(t8).start();
//new Thread(t8).start();

/**
* CPU不能及时切换,start只是启动,不能执行

*/
TestThread9 t9 = new TestThread9();
new Thread(t9).start();
try{Thread.sleep(1);}catch(Exception e){}
t9.s = "method";
new Thread(t9).start();

//一个是对象,一个是代码块,两者不同步


/*
while(bStop){
get
copy
}

bStop = true
*/
//数据库表复制,用多线程控制停止

}
}

class TestThread4 extends Thread{
int tickets = 100;
public void run(){
while(true){
if(tickets>0)
System.out.println(Thread.currentThread().getName() + " is saling ticket" + tickets--);
}
}
}

class TestThread5 implements Runnable{
int tickets = 100;
public void run(){
while(true){
if(tickets>0){
System.out.println(Thread.currentThread().getName() + " is saling ticket" + tickets--);
}
else
break;
}
}
}

class TestThread6 implements Runnable{
int tickets = 100;
public void run(){
while(true){
if(tickets>0){
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName() + " is saling ticket" + tickets--);
}
else
break;
}
}
}

class TestThread7 implements Runnable{
int tickets = 100;
String st = "123";
public void run(){
//对象不能放在run方法中,run会被调用4次,会产生4个对象
//但是string是同一个
//String st = "123";

while(true){
synchronized(st){
if(tickets>0){
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName() + " is saling ticket" + tickets--);
}
else
break;
}
}
}
}


class TestThread8 implements Runnable{
int tickets = 100;
public void run(){
while(true){
sale();
}
}
//不能保持同步
//public void sale(){
//if(tickets>0){
//try{Thread.sleep(10);}catch(Exception e){}
//System.out.println(Thread.currentThread().getName() + " is saling ticket" + tickets--);
//}
//}

//可以保持同步
//一个线程进入synchronized标志的方法中后
//其他所有的线程不能进入所有有synchronized标志的方法

public synchronized void sale(){
if(tickets>0){
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName() + " is saling ticket" + tickets--);
}
}
}

class TestThread9 implements Runnable{
int tickets = 100;
String s = new String("123");
public void run(){
if(s.equals("method")){
while(true){
sale();
}
}else{
while(true){
//同步对象
//synchronized(this);

synchronized(s){
if(tickets>0){
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName() + " is saling ticket" + tickets--);
}
}
}
}

}
public synchronized void sale(){
if(tickets>0){
try{Thread.sleep(10);}catch(Exception e){}
System.out.print("sale() : ");
System.out.println(Thread.currentThread().getName() + " is saling ticket" + tickets--);
}
}
}


public class ThreadDemo6 {
public static void main(String[] args){
//Thread控制器
TestThread12 t = new TestThread12();
new Thread(t).start();
for(int i=0;i<100;i++){
if(i==50){
t.stopMe();
}
System.out.println("main() is running");
}
}
}
class TestThread12 implements Runnable{
private boolean bStop =false;
public void stopMe(){
bStop = true;
}
@Override
public void run() {
while(!bStop){
System.out.println(Thread.currentThread().getName() + " is running");
}
}
}

原创粉丝点击