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的介绍,同样,更新也更频繁. ...
随机推荐
- MSSQLServer基础04(常用函数)
类型转换函数 CAST ( expression AS data_type) CONVERT ( data_type, expression,[style]) 对日期的转换.转换成各种国家格式的日期. ...
- 二维图形的矩阵变换(三)——在WPF中的应用矩阵变换
原文:二维图形的矩阵变换(三)--在WPF中的应用矩阵变换 UIElement和RenderTransform 首先,我们来看看什么样的对象可以进行变换.在WPF中,用于呈现给用户的对象的基类为Vis ...
- P127、面试题20:顺时针打印矩阵
题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字.例如:如果输入如下矩阵:1 2 3 4 5 6 7 89 10 11 1213 14 15 16则依次打印出 ...
- IIS会话过期的问题/WCF日志管理
http://technet.microsoft.com/zh-cn/library/cc725624(v=ws.10).aspx http://msdn.microsoft.com/zh-cn/li ...
- Android开发框架之xUtils学习
1.一个非作者弄的xUtils API文档: http://xutilsapi.oschina.mopaas.com/overview-summary.html 2.使用xUtils用户的一些博客文档 ...
- bzoj1858: [Scoi2010]序列操作
lazy-tag线段树. #include<cstdio> #include<algorithm> #include<cstring> using namespac ...
- [原]Unity3D深入浅出 - 物理材质(Physics Materials)
在Unity3d中已经配置好了5种常用的物理材质,Bouncy.Ice.Metal.Rubber.Wood,在菜单中依次选择Assets - Import Package - Physics Mate ...
- Apache HTTP Server多个拒绝服务漏洞
漏洞版本: Apache Group Apache HTTP Server < 2.4.9 漏洞描述: BUGTRAQ ID: 66303 CVE ID: CVE-2013-6438,CVE-2 ...
- Session简介
摘要:虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一技术.本文将详细讨论session的工作机制并且对在Java ...
- 实现类似QQ的即时通信程序(十一)
此为网络编程的一个系列,后续会把内容补上....