一、传统多线程

public void start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.It is never legal to start a thread more than once。

public void run()

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns. Subclasses of Thread should override this method.

 /* What will be run. */
private Runnable target;
public void run() {
if (target != null) {
target.run();
}
} public Thread(Runnable target) {
     init(null, target, "Thread-" + nextThreadNum(), 0);
 }

创建线程的两种方式:

//1.创建线程的方式一
Thread thread1 = new Thread(){
@Override
public void run() {
while(true){
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread1.start();
//2.创建线程的方式二
Thread thread2 = new Thread(new Runnable(){//更加体现面向对象的思想,将线程需要运行的代码装在一个Runnable对象中。
@Override
public void run() {
while(true){
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}});
thread2.start();

思考:

new Thread(new Runnable(){ //如果Thread的子类没有覆盖run方法,则会调用父类的run方法,父类的run方法会调用target对象的run方法
@Override
public void run() {
while(true){
System.out.println("runnable:"+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}){
public void run() {
while(true){
System.out.println("thread:"+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();

线程的互斥:(要让互斥访问代码块,要用同一个锁对象)

线程安全问题可以用银行转账来解释。

    class OutPutter{
public static synchronized void display1(String msg){//静态方法要同步也需要一个锁对象和它关联,静态方法运行时有个对象和它关联,那么这个对象是什么呢?类的字节码在内存中也是一个对象,静态方法运行时还没有类对象,但类的字节码对象已经在内存中了。
int len = msg.length();
for(int i = 0 ; i < len ; i++){
System.out.print(msg.charAt(i));
}
System.out.println();
}
public void display(String msg){
int len = msg.length();
synchronized (OutPutter.class) {
for(int i = 0 ; i < len ; i++){
System.out.print(msg.charAt(i));
}
System.out.println();
}
}
}

最新文章

  1. ENode框架Conference案例分析系列之 - Quick Start
  2. 滚动监听(bootstrap)
  3. mongodb php
  4. 用eclipse开发和调试postgresql-8.4.1
  5. 【Python自动化运维之路Day9】Socket
  6. Grid组件 列头居中
  7. 编译 proto 文件到指定语言的代码
  8. Qt学习之自定义窗口部件
  9. ZOJ3329之经典概率DP
  10. jQuery图片延迟加载插件
  11. 5_Navigation Bar
  12. Azure机器学习入门(四)模型发布为Web服务
  13. 手机自动化测试:appium源码分析之bootstrap八
  14. poj 2299 Ultra-QuickSort 题解
  15. 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)
  16. 网络编程之socketserver以及socket更多方法
  17. nodemanager 无法启动报错“doesn&#39;t satisfy minimum allocations”
  18. 爬虫IP代理中的http与https
  19. 【Codeforces 815C】Karen and Supermarket
  20. hdu 3966(树链剖分+线段树区间更新)

热门文章

  1. 使用java+TestNG进行接口回归测试
  2. linux 跟踪工具
  3. 【selenium+python】自动化测试目录与文件结构
  4. 通信协议之sdp---sdp会话协议
  5. 多媒体开发之---h264 图像参数级语义
  6. Android Apk包下查看 sha1
  7. HDU 5343 MZL&#39;s Circle Zhou 后缀自动机+DP
  8. 【BZOJ3544】[ONTAK2010]Creative Accounting 前缀和+set
  9. mysq&#39;l系列之10.mysql优化&amp;权限控制
  10. sort()函数到底是怎样进行数字排序的