java创建线程两种方式:

1.继承Thread创建线程

/**
* Created by lsf on 16/4/18.
*/
class NewThread extends Thread {
NewThread(){
super(); //创建线程
start(); //启动线程
} public void run() {
long starttime = System.currentTimeMillis();
System.out.println("child thread..."+starttime);
}
} class CurrentThreadDemo {
public static void main(String args[]) {
long starttime2 = System.currentTimeMillis();
System.out.println("main thread,,,"+starttime2); //主线程
new NewThread();
System.out.println("住县城");
}
}

2.实现

import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;

import java.util.Date;

/**
* Created by lsf on 16/4/18.
*/
class NewThread extends Thread {
NewThread(){
Thread t = new Thread(this); //创建线程
t.start(); //启动线程
} public void run() {
long starttime = System.currentTimeMillis();
System.out.println("child thread..."+starttime);
}
} class CurrentThreadDemo {
public static void main(String args[]) {
long starttime2 = System.currentTimeMillis();
System.out.println("main thread,,,"+starttime2);
new NewThread();
System.out.println("主线程"); //主线程
}
}

3.给任务创建多个线程去执行

class NewThread extends Thread {
String name;
NewThread(String threadname){
name = threadname;
Thread t = new Thread(this,threadname); //创建线程
t.start(); //启动线程
} public void run() {
long starttime = System.currentTimeMillis();
System.out.println("child thread..."+name);
}
} class CurrentThreadDemo {
public static void main(String args[]) {
long starttime2 = System.currentTimeMillis();
System.out.println("main thread,,,"+starttime2);
new NewThread("demo1");
new NewThread("demo2");
new NewThread("demo3");
System.out.println("主线程"); //主线程
}
}

4.线程优先级设置

/**
* Created by lsf on 16/4/22.
*/ class ThreadTest implements Runnable { Thread t;
int count = 0;
private volatile Boolean flag = true; public ThreadTest(int p) {
t = new Thread(this);
t.setPriority(p);
} public void start(){
t.start();
} public void finish(){
flag = false;
} @Override
public void run() {
while(flag){
count++;
}
} } public class ThreadPriority {
public static void main(String[] args) {
ThreadTest t1 = new ThreadTest(Thread.NORM_PRIORITY - 2);
ThreadTest t2 = new ThreadTest(Thread.NORM_PRIORITY + 2);
t1.start();
t2.start();
t1.finish();
t2.finish();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} try {
System.out.println("t1 count:"+t1.count);
System.out.println("t2 count:"+t2.count);
t1.t.join();
t2.t.join();
System.out.println("t1 is alive:" + t1.t.isAlive());
System.out.println("t2 is alive:" + t1.t.isAlive());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

5.线程同步

线程同步的关键在于同一时刻线程在管程内,应用场景一般是:当某个方法(callme)需要用多线程去执行,可以改造一下对应的方法,加上关键词synchronized,这样在调用过程中,每个线程都会默认进入隐式管程。

/**
* Created by root on 16-4-15.
*/ class Callme {
synchronized void call(String msg){
System.out.println("["+msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("]");
}
} class ThreadCaller implements Runnable{ String msg;
Callme target;
Thread t; public ThreadCaller(Callme targ,String s) {
target = targ;
msg = s;
t = new Thread(this);
t.start();
} @Override
public void run() {
target.call(msg);
}
} class Demo {
public static void main(String[] args) {
Callme target = new Callme();
ThreadCaller obj1 = new ThreadCaller(target,"Hello");
ThreadCaller obj2 = new ThreadCaller(target,"Synchronized");
ThreadCaller obj3 = new ThreadCaller(target,"World"); try {
obj1.t.join();
obj2.t.join();
obj3.t.join();
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
}

  

java线程跟多线程的更多相关文章

  1. Java线程与多线程教程

    本文由 ImportNew - liken 翻译自 Journaldev.   Java线程是执行某些任务的轻量级进程.Java通过Thread类提供多线程支持,应用可以创建并发执行的多个线程. 应用 ...

  2. Java线程和多线程(十三)——Callable,Future,FutureTask

    在Java多线程之中,Callable和Future的使用时非常广泛的.在之前的文章中,我们了解了关于Java线程池基础的一些内容,知道如何提交Runnable的任务.但是,Runnable的任务是无 ...

  3. Java线程和多线程(十二)——线程池基础

    Java 线程池管理多个工作线程,其中包含了一个队列,包含着所有等待被执行的任务.开发者可以通过使用ThreadPoolExecutor来在Java中创建线程池. 线程池是Java中多线程的一个重要概 ...

  4. Java线程和多线程(三)——线程安全和同步

    线程安全在Java中是一个很重要的课题.Java提供的多线程环境支持使用Java线程.我们都知道多线程共享一些对象实例的话,可能会在读取和更新共享数据的事后产生数据不一致问题. 线程安全 之所以会产生 ...

  5. Java线程和多线程(一)——线程的基本概念

    Java 线程是一个轻量级执行任务的处理单元.Java提供了Thread类来支持多线程,开发者在应用中可以创建多个线程来支持并发执行任务. 在应用中存在两种类型的线程,用户线程和守护线程.当我们启动应 ...

  6. Java 线程与多线程

    Java是一门支持多线程的编程语言! 什么是进程? 计算机中内存.处理器.IO等资源操作都要为进程进行服务. 一个进程上可以创建多个线程,线程比进程更快的处理单元,而且所占用的资源也小,多线程的应用也 ...

  7. Java线程和多线程(八)——Thread Dump

    Java的Thread Dump就是列出JVM中所有激活状态的线程. Java Thread Dump Java Thread Dump在分析应用性能瓶颈和死锁的时候,是非常有效的. 下面将介绍多种不 ...

  8. Java 线程和多线程执行过程分析

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  9. Java线程和多线程(十五)——线程的活性

    当开发者在应用中使用了并发来提升性能的同时,开发者也需要注意线程之间有可能会相互阻塞.当整个应用执行的速度比预期要慢的时候,也就是应用没有按照预期的执行时间执行完毕.在本章中,我们来需要仔细分析可能会 ...

随机推荐

  1. 第 2 章 VBScript基本概念

    学习导航 VBScript 基本知识 变量.常量.数组 算术.逻辑.比较 运算符 2.1 VBScript是什么 VBScript程序语言是Microsoft公司VB(Visual Basic)程序语 ...

  2. PHP中new static()与new self()的比较

    今天在coding的时候,发现了 new static(),觉得实例化的地方不是应该是 new self()吗?查询了一下才知道两者的区别: 1)在有子类集成的时候,两者的表现不一样 2)php 5. ...

  3. ASP.NET Redis 开发

    文件并发(日志处理)--队列--Redis+Log4Net Redis简介 Redis是一个开源的,使用C语言编写,面向“键/值”对类型数据的分布式NoSQL数据库系统,特点是高性能,持久存储,适应高 ...

  4. 001.Getting Started -- 【入门指南】

    Getting Started 入门指南 662 of 756 people found this helpful Meng.Net 自译 1. Install .NET Core 到官网安装 .NE ...

  5. github指令

    一般用法 git stash git pull git stash pop 结局冲突 git add . git commit -m "message" git push 查看 g ...

  6. react引用ant的table组件

    import React from 'react';import '../../css/uicss/UI.css';import 'antd/lib/style/index.less';import ...

  7. Python开发【第三篇】:Python基本之文件操作

    Python基本之文本操作 一.初识文本的基本操作 在python中打开文件有两种方式,即:open(...) 和  file(...) ,本质上前者在内部会调用后者来进行文件操作,推荐使用 open ...

  8. Google C++单元测试框架GoogleTest---TestFixture使用

    一.测试夹具(Test Fixtures):对多个测试使用相同的数据配置 如果你发现自己写了两个或更多的测试来操作类似的数据,你可以使用测试夹具.它允许您为几个不同的测试重复使用相同的对象配置. 要创 ...

  9. Java Web中请求转发和请求包含

    1.都是在一个请求中跨越多个Servlet 2.多个Servlet在一个请求中,他们共享request对象.就是在AServle中setAttribute()保存数据在BServlet中由getAtt ...

  10. xcode svn commit is not under version control (1) & git commit

    使用Xcode提交一个第三方库时,由于包含资源文件,总是提交不了,提示报错:XXX commit is not under version control (1) 网上查了下,得知 xcode对于sv ...