`
x125521853
  • 浏览: 71556 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

多线程笔记

阅读更多

一:多线程

    一个进程中又可以包含一个或多个线程,一个线程就是一个程序内部的一条执行线索,
如果要一个程序中实现多段代码同时交替运行,就需产生多个线程,并指定每个线程上所
要运行的程序代码段,就是多线程。

 

二:单线程实例:

class TestThread
{
	public void run(){
		while(true){
			System.out.println("run():"+Thread.currentThread().getName());
		}
	}
}

class ThreadDemo1
{
	public static void main(String[] args){
		new TestThread().run();
		while(true){
			System.out.println("main():"+Thread.currentThread().getName());
		}
	}
}

 

三:多线程实例:

class TestThread extends Thread
{
	public void run(){
		while(true){
			System.out.println("run():"+Thread.currentThread().getName());
		}
	}
}

class ThreadDemo1
{
	public static void main(String[] args){
		new TestThread().start();
		while(true){
			System.out.println("main():"+Thread.currentThread().getName());
		}
	}
}

   注: main()和run()代码交替执行,在main()返回线程名为main,run()返回线程名为Thread-0

 

四:用thread类创建线程

   1.要将一段代码在一个新的线程上运行,该代码应该在一个类的run函数中,并且run函数所在的类是Thread类的子类。
   2.启动一个新的线程,我们不是直接调用Thread的子类对象的run方法,而是调用Thread子类对象的start()方法。Thread
  类对象的start()方法将产生一个新的线程,并且在该线程上运行该Thread类对象中的run()方法。

 

五:后台线程

   1.如果我们对某个线程对象在启动(调用start()方法)之前调用了setDaemon(true)方法,这个线程就变成了后台线程。

   2.前台多线程实例:

class TestThread extends Thread
{
	public void run(){
		while(true){
			System.out.println("run():"+Thread.currentThread().getName());
		}
	}
}

class ThreadDemo1
{
	public static void main(String[] args){
		new TestThread().start();		
	}
}

  注:只要还有一个前台线程在运行,整个进程都不会结束。

   3.后台多线程实例:

class TestThread extends Thread
{
	public void run(){
		while(true){
			System.out.println("run():"+Thread.currentThread().getName());
		}
	}
}

class ThreadDemo1
{
	public static void main(String[] args){
		Thread tt = new TestThread();
		tt.setDaemon(true);
		tt.start();
	}
}

   注:只要还有一个前台线程在运行,这个线程就不会结束,如果一个进程中只有后台线程运行,这个进程就会结束。

 

六:使用Runnable接口创建多线程

     1.适合多个相同程序代码的线程去处理同一个资源的情况,把虚拟CPU(线程)同程序的代码、数据有效分离。

    2. 四个线程共卖1000张票

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

class ThreadDemo1
{
	public static void main(String[] args){
		TestThread tt = new TestThread();
		new Thread(tt).start();
		new Thread(tt).start();
		new Thread(tt).start();
		new Thread(tt).start();
	}
}

 

七:线程同步

class TestThread implements Runnable {
	int tickets = 200;
	String str = new String("");

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

class ThreadDemo1
{
	public static void main(String[] args){
		TestThread tt = new TestThread();
		new Thread(tt).start();
		new Thread(tt).start();
		new Thread(tt).start();
		new Thread(tt).start();
	}
}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics