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();
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的介绍,同样,更新也更频繁. ...
随机推荐
- HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)
Invitation Cards DescriptionIn the age of television, not many people attend theater performances. A ...
- 安装Hadoop系列 — 新建MapReduce项目
1.新建MR工程 依次点击 File → New → Ohter… 选择 “Map/Reduce Project”,然后输入项目名称:mrdemo,创建新项目: 2.(这步在以后的开发中可能 ...
- CentOS7.1 安装关键步骤 记录下来
SecureCRT下载地址 https://yunpan.cn/cS9W94kuvhXPb 访问密码 08cd[这里GNOME桌面 下的 要全选,截屏有误]
- Wince6 RIL层移植
RIL移植: 因为不同的模组,支持的AT命令有所不同,或是格式不一样,还有就是返回不一样,我们有必要对不同的模组进行RIL驱动移植. 在Response.cpp,Msg.cpp等 文件中ParseXX ...
- MAX-HEAPIFY(2/3n的疑惑)
Q: In CLRS, third Edition, on page 155, it is given that in MAX-HEAPIFY, The children’s subtrees eac ...
- 函数buf_LRU_free_from_unzip_LRU_list
/******************************************************************//** Try to free an uncompressed ...
- tyvj1161聚会的名单(trie树)
背景 Background 明天就是candy的生日,candy又会邀请自己的一大堆好友来聚会了!哎!又要累坏飘飘乎居士了!! 描述 Description 明天就是candy的生日.晚上,c ...
- UVA 12661 Funny Car Racing 有趣的赛车比赛(最短路,变形)
题意:赛道有n个交叉点,和m条单向路径(有重边),每条路都是周期性关闭的,且通过仍需一段时间.在比赛开始时,所有道路刚好打开,选择进入该道路必须满足“在打开的时间段进入,在关闭之前出来”,即不可在路上 ...
- c#里BindingFlags 筛选标志
下列 BindingFlags 筛选标志可用于定义包含在搜索中的成员: 为了获取返回值,必须指定 BindingFlags.Instance 或 BindingFlags.Static. 指定 Bin ...
- 常用ping服务列表
以下是收集的ping服务器. ping是基于XML_RPC标准协议的更新通告服务,是用于blog在内容更新时通知博客搜索引擎及时进行抓取.更新的方式.博客搜索引擎在成功接受到ping以后,会立刻进行抓 ...