一:多线程
一个进程中又可以包含一个或多个线程,一个线程就是一个程序内部的一条执行线索,
如果要一个程序中实现多段代码同时交替运行,就需产生多个线程,并指定每个线程上所
要运行的程序代码段,就是多线程。
二:单线程实例:
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();
}
}
分享到:
相关推荐
多线程学习笔记,好资源。包括线程基础等知识多线程学习笔记,好资源。包括线程基础等知识
马士兵多线程训练营上课笔记
Java线程的知识点总结。doc
C# 多线程 笔记 适合 初学者 共含所有7个word文档,转载
C#多线程笔记 每个窗体都有自己的都在不同的线程上运行,如果需要在窗体之间交互,就需要在线程之间交互。
C#多线程笔记学习指南
java多线程笔记分享
详细的Java多线程笔记,一共17章,能够让你更加了解多线程.线程是程序中的一个执行流,每个线程都有自己的专有寄存器(栈指针、程序计数器等),但代码区是共享的,即不同的线程可以执行同样的函数。
java中的多线程笔记
使用java.lang.Thread类或者java.lang.Runnable接口编写代码来定义、实例化和启动新线程。 一个Thread类实例只是一个对象,像Java中的任何其他对象一样,具有变量和方法,生死于堆上。 Java中,每个线程都有一个...
Python3 多线程
马士兵多线程训练营笔记
多线程学习笔记 包含doc文档笔记加笔记中出现的代码(博文地址共上中下)http://blog.csdn.net/cloudyxuq/article/details/6941404
java中数据流和多线程较深入的了解,这是分享别人的学习笔记
对多线程进行全面学习的笔记,里面包含简单到复杂的多线程的基础知识