java学习之路---线程(重点)
class
ThreadDemo
extends
Thread{
private
String
userName
;
public
String getUserName() {
return
userName
;
public
void
setUserName(String userName) {
this
.
userName
= userName;
public
ThreadDemo(String userName) {
this
.
userName
= userName;
@Override
public
void
run() {
for
(
int
i=1;i<10;i++){
System.
out
.println(
userName
+i);
public
static
void
main(String[] args) {
new
ThreadDemo (
"线程A"
);
new
ThreadDemo (
"线程B"
);
class
ThreadDemo
extends
Thread{
private
String
userName
;
public
String getUserName() {
return
userName
;
public
void
setUserName(String userName) {
this
.
userName
= userName;
public
ThreadDemo(String userName) {
this
.
userName
= userName;
@Override
public
void
run() {
for
(
int
i=1;i<10;i++){
out
.println(
userName
+i);
public
static
void
main(String[] args) {
new
ThreadDemo (
"线程A"
);
new
ThreadDemo (
"线程B"
);
public
synchronized
void
start() {
/**
if
(
threadStatus
!= 0)
throw
new
IllegalThreadStateException();
/* Notify the group that this thread is about to be started
group
.add(
this
);
boolean
started =
false
;
try
{
true
;
finally
{
try
{
if
(!started) {
group
.threadStartFailed(
this
);
catch
(Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
private
native
void
start0();
class
ThreadDemo
extends
Thread{
private
String
userName
;
public
String getUserName() {
return
userName
;
public
void
setUserName(String userName) {
this
.
userName
= userName;
public
ThreadDemo(String userName) {
this
.
userName
= userName;
@Override
public
void
run() {
for
(
int
i=1;i<10;i++){
out
.println(
userName
+i);
public
static
void
main(String[] args) {
new
ThreadDemo(
"线程A"
);
new
ThreadDemo(
"线程B"
);
java.lang.IllegalThreadStateException
Thread.java:682
)
ThreadDemo.java:34
)
MyTread
implements
Runnable{
private
Integer
ticket
=10;
public
void
run() {
public
synchronized
void
fun(){
for
(
int
i=0;i<100;i++){
if
(
ticket
>0){
out
.println(
"卖票: ticket="
+
ticket
--);
class
ThreadDemo1{
public
static
void
main(String[] args) {
new
MyTread();
new
Thread(myTread).start();
new
Thread(myTread).start();
new
Thread(myTread).start();
MyTread
implements
Runnable{
private
Integer
ticket
=10;
public
void
run() {
for
(
int
i=0;i<100;i++){
if
(
ticket
>0)
out
.println(
"票号"
+
ticket
--);
class
ThreadDemo1{
public
static
void
main(String[] args) {
new
MyTread();
new
Thread(myTread).start();
new
Thread(myTread).start();
new
Thread(myTread).start();
.png)
.png)
MyTread
implements
Runnable{
private
Integer
ticket
=10;
public
void
run() {
for
(
int
i=0;i<100;i++){
if
(
ticket
>0)
out
.println(Thread.currentThread().getName()+
"票号"
+
ticket
-- );//取得线程名称,如果自己没有给名称,那么程序就会自动付给一个名称
public
Thread (Runnable target) {
null
, target,
"Thread-"
+ nextThreadNum(), 0);
private
static
synchronized
int
nextThreadNum () {
return
threadInitNumber
++;
class
ThreadDemo1 {
public
static
void
main(String[] args) {
new
MyTread();
new
Thread(myTread).start();
new
Thread(myTread).start();
new
Thread(myTread).start();
MyTread
implements
Runnable{
private
Integer
ticket
=10;
public
void
run() {
for
(
int
i=0;i<100;i++){
if
(
ticket
>0)
out
.println(Thread.currentThread().getName()+
"票号"
+
ticket
-- );
class
ThreadDemo1 {
public
static
void
main(String[] args) {
new
MyTread();
new
Thread(myTread,
"A线程"
).start();
//自己给线程名称
new
Thread(myTread,
"like"
).start();
new
Thread(myTread).start();
MyTread
implements
Runnable{
private
Integer
ticket
=10;
public
void
run() {
for
(
int
i=0;i<100;i++){
if
(
ticket
>0){
try
{
catch
(InterruptedException e) {
out
.println(Thread.currentThread().getName()+
"票号"
+
ticket
-- );
class
ThreadDemo1 {
public
static
void
main(String[] args) {
new
MyTread();
new
Thread(myTread,
"A线程"
).start();
//自己给线程名称
new
Thread(myTread,
"like"
).start();
new
Thread(myTread).start();
ticket
-- ,而另一个线程就已经进入
MyTread
implements
Runnable{
private
Integer
ticket
=10;
public
void
run() {
for
(
int
i=0;i<100;i++){
if
(
ticket
>0){
try
{
catch
(InterruptedException e) {
out
.println(Thread. currentThread().getName()+
"票号"
+
ticket
-- );
class
ThreadDemo2 {
public
static
void
main(String[] args) {
new
MyTread();
new
Thread(myTread,
"A线程"
);
//自己给线程名称
out
.println (
"线程是否启动"
+t.isAlive());
//启动线程
out
.println (
"线程是否启动"
+t.isAlive());
new
Thread(myTread,
"like"
).start();
new
Thread(myTread).start();
MyTread
implements
Runnable{
private
Integer
ticket
=10;
public
void
run() {
for
(
int
i=0;i<100;i++){
if
(
ticket
>0){
try
{
catch
(InterruptedException e) {
out
.println(Thread.currentThread().getName()+
"票号"
+
ticket
-- );
class
ThreadDemo2 {
public
static
void
main(String[] args) {
new
MyTread();
new
Thread(myTread,
"A线程"
);
//自己给线程名称
out
.println(
"线程是否启动"
+t.isAlive());
//启动线程
for
(
int
i=0;i<10;i++){
if
(i>2){
try
{
//线程的强制惊醒
catch
(InterruptedException e) {
//
TODO
Auto-generated catch block
out
.println(
"线程是否启动"
+t.isAlive());
new
Thread(myTread,
"like"
).start();
new
Thread(myTread).start();
MyTread1
implements
Runnable{
private
Integer
ticket
=10;
public
void
run() {
for
(
int
i=0;i<100;i++){
if
(
ticket
>0){
try
{
catch
(InterruptedException e) {
out
.println(
"A线程被中止"
);
return
;
out
.println(Thread.currentThread().getName()+
"票号"
+
ticket
-- );
class
ThreadDemo2 {
public
static
void
main(String[] args) {
new
MyTread1();
new
Thread(myTread,
"A线程"
);
//自己给线程名称
out
.println(
"线程是否启动"
+t.isAlive());
//启动线程
try
{
catch
(InterruptedException e) {
//
TODO
Auto-generated catch block
out
.println(
"线程是否启动"
+t.isAlive());
new
Thread(myTread,
"like"
).start();
new
Thread(myTread).start();
true
);
MyTread1
implements
Runnable{
private
Integer
ticket
=10;
public
void
run() {
for
(
int
i=0;i<100;i++){
if
(
ticket
>0){
try
{
catch
(InterruptedException e) {
out
.println(
"A线程被中止"
);
out
.println(Thread.currentThread().getName()+
"票号"
+
ticket
-- );
class
ThreadDemo2 {
public
static
void
main(String[] args) {
new
MyTread1();
new
Thread(myTread,
"A线程"
);
//自己给线程名称
new
Thread(myTread,
"like"
);
new
Thread(myTread);
MAX_PRIORITY
);
//设置线程优先级,最大10
MIN_PRIORITY
);最小1
NORM_PRIORITY
);5
//启动线程
class
ThreadDemo2 {
public
static
void
main(String[] args) {
out
.println(Thread.currentThread().getPriority());
//获取主方法的优先级
MyTread1
implements
Runnable{
private
Integer
ticket
=10;
public
void
run() {
for
(
int
i=0;i<100;i++){
if
(
ticket
>0){
out
.println(Thread.currentThread().getName()+
"----->"
);
//获取线程的名字
if
(
ticket
==3){
out
.print(
"线程礼让:"
);
out
.println(Thread.currentThread().getName()+
"票号"
+
ticket
-- );
class
ThreadDemo2 {
public
static
void
main(String[] args) {
new
MyTread1();
new
Thread(myTread,
"A线程"
);
//自己给线程名称
new
Thread(myTread,
"like"
);
new
Thread(myTread);
MAX_PRIORITY
);
//设置线程优先级
MIN_PRIORITY
);
NORM_PRIORITY
);
//启动线程
java学习之路---线程(重点)的更多相关文章
- Java学习之路(转)
Java学习之路(书籍推荐) 一.基础类 1.<Thinking in java>(阅读2遍),入门第一位是建立正确的概念 2.<Core Java>这本书更贴近实践,更多 ...
- 新篇章之我的java学习之路下
昨天写下了人生的第一篇博客,今天接着写我的java学习之路有关开发及框架的学习过程. 想要学好java语言,只学习一些java的基本语法对实际开发中的用处还是不大的,所以我们还要掌握一些有关javaW ...
- java学习之路之javaSE基础2
java学习之路之javaSE基础2 所有的代码都是引用他人写的. 1.逻辑运算符 //&,|,^,! //int x = 10; //5 < x < 15 //x > 5 ...
- java学习之路之javaSE基础1
<h2>java学习之路之javaSE基础1</h2> <div> ###01.01_计算机基础知识(计算机概述)(了解)* A:什么是计算机?计算机在生活中的应用 ...
- java学习笔记15--多线程编程基础2
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note15.html,转载请注明源地址. 线程的生命周期 1.线程的生命周期 线程从产生到消亡 ...
- JAVA学习之路与大家分享
这是我四年以前总结的JAVA学习之路,希望对初学者或想从事JAVA开发的人有帮助. 本人是软件工程专业出身,先后学过C.C++.JAVA.VB.delphi等等开发语言以及网络相关管理技术.哎,好久不 ...
- Java 学习之路 -- day00
Java 学习之路 -- day00 Typora 快捷键操作 标题:#+空格 2. *斜体* 3. **加粗** 4. **斜体加粗*** 5. ~~删除线~~ 6. > 引用 7. ···分 ...
- (转)如何学习Java技术?谈Java学习之路
51CTO编者注:这篇文章已经是有数年“网龄”的老文,不过在今天看来仍然经典.如何学习Java?本篇文章可以说也是面对编程初学者的一篇指导文章,其中对于如何学习Java的步骤的介绍,很多也适用于开发领 ...
- Java学习之路(书籍推荐)
一.基础类 1.<Thinking in java>(阅读2遍),入门第一位是建立正确的概念 2.<Core Java>这本书更贴近实践,更多API的介绍,同样,更新也更频繁. ...
随机推荐
- Bridging signals ZOJ 3627 POJ1631 HDU1950
题意:给出一个从1-n的数字排列,求最长上升子序列长度. 直接说解法吧.新开一个数组d,d[i]表示的是能构成长度为i的上升子序列的在原序列中最后的那个数值.程序的主要过程:当循环到第i个的时候,如果 ...
- 再探Tomcat
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmQAAADyCAIAAABs006cAAAgAElEQVR4nO2d228b1RaH+2/l9RQhQE
- Python之模块篇
简介 你已经学习了如何在你的程序中定义一次函数而重用代码.如果你想要在其他程序中重用很多函数,那么你该如何编写程序呢?你可能已经猜到了,答案是使用模块.模块基本上就是一个包含了所有你定义的函数和变量的 ...
- JavaScript定时器详解及实例
JS里设定延时: 使用SetInterval和设定延时函数setTimeout 很类似.setTimeout 运用在延迟一段时间,再进行某项操作. setTimeout("function& ...
- 常用PHP框架功能对比表
自接触PHP开发以来,已使用了不少框架,虽然对每个框架都没有专研至深,但对每一款所使用的PHP框架功能都有一些了解的.至此,本人将大家常用的一些PHP框架功能整理成表,希望对大家在选择PHP框架时,可 ...
- 学习笔记-[Maven实战]-第三章:Maven使用入门(3)
这里说一下在建测试工程里遇到的问题 1.第一次建工程,junit依赖始终没有成功,最后删除现在工程,新建了一个工程就好了 2.使用junit4的问题.工程默认的依赖是junit3.8.1,我改成了4. ...
- 摄像头(5)使用Camera2 替代过时的Camera API
转自: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0428/2811.html 概要 从5.0开始(API Level 21 ...
- Java面试题-并发框架
1. SynchronizedMap和ConcurrentHashMap有什么区别? 答:java5中新增了ConcurrentMap接口和它的一个实现类ConcurrentHashMap.Conc ...
- Unity3D GUI中的图片跟随鼠标旋转脚本
var Mid : Texture2D; var mouse : Texture2D; //鼠标图片 var mousePs = Vector2.zero; //鼠标的位置 private var a ...
- ajax withCredentials在firefox下问题的解释
1,起因: 跨域的问题一般有两种解决方式比较常用,一是使用jsonp,二是服务端配置cors策略.至于jsonp这里不再赘述,本文主要解释一下cors解决跨域产生的问题 2,cors跨域产生的问题 j ...