【Java学习笔记】线程

------Java培训、Android培训、iOS培训、.Net培训 期待与您交流!-------


今天查看API学习了线程的概念还有用法。

public class Thread
extends Object
implements Runnable
A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:
The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.
There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started.The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started. 

公共类Thread继承自对象类,实现了Runable接口。

线程是程序中执行的一个进程。Java虚拟机允许一个应用中有多个线程并行执行。

每一个线程都有优先级。高优先级的线程会先被执行。每一个线程都有可能被标记为守护线程。当在线程内运行代码时会创建一个新的线程对象,新的线程将拥有和被创建的线程同等的优先级,并且当且仅一个线程创建的线程是守护线程时,那么这个线程才是守护线程。


在Java虚拟机启动的时候,通常都会有一个非单一的守护线程(一般调用指定类的方法)。Java虚拟机会继续执行线程,直到出现以下情况:

1>Runtime类的退出方法被调用,并且安全管理器运行退出操作被执行时

2>无论是从调用run方法中返回的,或是超出run方法而抛出的异常而产生的所有非守护线程都已经结束。

有两种方法可以创建一个新的线程。一是声明一个Thread类的子类。这个子类要重写Thread类的run方法。实例化的子类可以被分配和启动。另一个方法是声明一个实现Runnable接口的类,然后让该类实现run方法。实例化的子类可以被分配,作为参数传递,启动。


下面是简单实例:

package exp;

/**
 * 线程简单实例
 * 
 * @author VisionDo
 *
 */
public class ThreadDemo {

	public static void main(String[] args) {
		MyThread myth = new MyThread();
		MyThread2 myth2 = new MyThread2();
		
		Thread th = new Thread(myth);//创建线程
		th.start();//启动线程
		
		th = new Thread(myth2);//创建线程
		th.start();//启动线程
		
	}

}


/**
 * 
 * 通过继承Thread,重写run方法得到线程
 *
 */
class MyThread extends Thread{

	@Override
	public void run() {
		super.run();
		String s = "通过继承Thread,重写run方法生成线程";
		System.out.println(s);
	}
		
}

/**
 * 
 * 通过实现Runnable接口,重写run方法得到线程
 *
 */
class MyThread2 implements Runnable{

	@Override
	public void run() {
		String s = "通过实现Runnable接口,重写run方法生成线程";
		System.out.println(s);
	}
	
}
运行后输出:



可以看出,run方法内的语句都被打印出来了。执行顺序与线程的start方法一致。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Thinking in Java》是一本经典的Java编程入门教材,通过阅读该书可以系统地学习Java编程的基础知识和高级概念。阅读过程中,我结合自己的学习体验和实践,进行了详细的学习笔记。 首先,该书从基础语法开始介绍,包括数据类型、控制语句、数组等。对于初学者来说,这些基础知识是最基本的,通过书中的示例代码和解析,我能够更好地理解这些概念和语法规则。 其次,书中对面向对象编程进行了深入的讲解。通过学习面向对象的思想,我明白了类、对象、继承、多态等概念的含义和使用方法。同时,书中还讲解了接口、内部类、异常处理等较为高级的概念,极大地拓宽了我的Java知识面。 另外,该书还介绍了Java的常见类库和工具,如字符串操作、集合框架、输入输出、线程等。这些内容对于实际开发非常有用,而且书中的示例代码也能帮助我更好地理解和应用这些类库和工具。 此外,该书通过大量的实例和案例,生动地展示了Java编程的实战应用。这些实例涵盖了各种不同的应用场景,从简单的程序到复杂的项目,都有所涉及。通过分析这些实例,我不仅可以学习到实际编程的技巧,还能提高自己的解决问题的能力。 总的来说,读完《Thinking in Java》后,我对Java编程有了更深入的理解和掌握。通过学习笔记的整理,我不仅复习了各个知识点,还加深了对这些概念的理解。希望这份学习笔记能够帮助到其他想要学习Java的同学,让他们能够更快地入门和掌握这门编程语言。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值