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的介绍,同样,更新也更频繁. ...
随机推荐
- Android Paint中setTextSize
界面适配的时候发现Paint.setTextSize与TextView.setTextSize传入的单位不一致.Paint.setTextSize传入的单位是px,TextView.setTextSi ...
- xml格式化
Vim怎么格式化xml,完全不会,vim的缩进也搞不明白
- Android开源滤镜 仿instagram
前段时间做一个项目的时候发现一个不错的滤镜库,是仿Instagram效果的,能够实现Lomo在内的十几种滤镜效果,git地址是: https://github.com/beartung/insta-f ...
- ServletContext对象
**1 ServletContext对象 1)在web应用中,由服务器创建的唯一的一个对象是ServletContext 2)ServletContext对象在每一个Servlet中取得都是相 ...
- 摄像头(3)调用系统拍照activity来拍照
import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager ...
- 【HDOJ】2459 Maximum repetition substring
后缀数组+RMQ. /* 2459 */ #include <iostream> #include <sstream> #include <string> #inc ...
- hdu2642Fliping game
http://acm.hdu.edu.cn/showproblem.php?pid=4642 这题..刚一看以为是什么高深的博弈 后来看过的人挺多 想是不是有什么规律 结果理解错题意了 以为随便圈一矩 ...
- UVa 10214 (莫比乌斯反演 or 欧拉函数) Trees in a Wood.
题意: 这道题和POJ 3090很相似,求|x|≤a,|y|≤b 中站在原点可见的整点的个数K,所有的整点个数为N(除去原点),求K/N 分析: 坐标轴上有四个可见的点,因为每个象限可见的点数都是一样 ...
- UVa 1641 ASCII Area
题意: 就是用一个字符矩阵代表一个闭合的阴影部分,然后求阴影部分的面积. 分析: 一个'/'和'\'字符都代表半个小方块的面积. 关键就是判断'.'是否属于阴影部分,这才是本题的关键. 从第一列开始, ...
- 【转】UIColor对颜色的自定义
原文网址:http://blog.sina.com.cn/s/blog_5f19ccb10101bhqh.html 在iOS开发中,我们使用UIColor来对我们的界面进行颜色设置,一般我们通过以下两 ...