package com.aaa.Threadday2;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ThreadTest {
public static void main(String[] args) {
}
}
//创建线程的第一种方法:继承Thread类
class ThreadTest01{
public static void main(String[] args) {
MyThread01 t = new MyThread01();
t.setName("t");
t.start();//启动线程
for (int i = 0; i < 1000; i++){
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
class MyThread01 extends Thread{
@Override
public void run() {
for (int i = 0; i < 1000; i++){
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
//创建线程的第二种方法:实现Runnable接口
class ThreadTest02Two{
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable01(), "t");//创建线程并设置名字
t.start();
for (int i = 0; i < 1000; i++){
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
// 这并不是一个线程类,是一个可运行的类。它还不是一个线程。
class MyRunnable01 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 1000; i++){
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
//创建线程的第二种方法:实现Runnable接口(采用匿名内部类)
class ThreadTest03Two{
public static void main(String[] args) {
//匿名内部类
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000; i++){
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
});
t.setName("t");
t.start();
for (int i = 0; i < 1000; i++){
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
/**
* Thread.currentThread()获取当前线程对象(静态方法)
* 线程.getName()获取当前线程名字
* 线程.setName()设置当前线程名字
*/
class ThreadTest04Two{
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName());//当前线程名字 main
MyThread01 t1 = new MyThread01();
MyThread01 t2 = new MyThread01();
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
for (int i = 0; i < 1000; i++){
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
//sleep(long millis)(静态方法)
class ThreadTest05{
public static void main(String[] args) {
for (int i = 0; i < 10; i++){
try {
Thread.sleep(1000);//睡眠1s
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
//interrupt()中断正在睡眠的线程(不推荐使用,了解即可)
class ThreadTest06Two {
public static void main(String[] args) {
MyThread02 t = new MyThread02();
t.setName("t");
t.start();
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("hello world");
t.interrupt();
}
}
class MyThread02 extends Thread{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "--->begin" );
try {
Thread.sleep(1000 * 60 * 60 * 24 * 365);//睡一年
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "--->end" );
}
}
//stop()终止一个线程执行(不推荐使用,可能导致数据丢失)
class ThreadTest07{
public static void main(String[] args) {
MyThread03 t = new MyThread03();
t.setName("t");
t.start();
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.stop();
}
}
class MyThread03 extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++){
System.out.println(Thread.currentThread().getName() + "--->" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//合理终止一个线程:设置一个标记
class ThreadTest08Two{
public static void main(String[] args) {
MyThread04 t = new MyThread04();
t.setName("t");
t.start();
try {
Thread.sleep(1000 * 9);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("hello world");
// 终止线程
// 你想要什么时候终止t的执行,那么你把标记修改为false,就结束了。
t.flag = true;
}
}
class MyThread04 extends Thread{
boolean flag = false;
@Override
public void run() {
if (this.flag){
return ;
}
for (int i = 0; i < 1000; i++){
if (this.flag){//由于一个线程一直运行此程序,要是判断在外面只会在启动线程时判断并不会结束,
// 因此需要每次循环判断一下标记。
/**
* 这里可以保存东西
*/
return ;
}
System.out.println(Thread.currentThread().getName() + "--->" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/*//MyThread04另一种写法
class MyThread04 extends Thread{
boolean flag = false;
@Override
public void run() {
for (int i = 0; i < 1000; i++){
if (!this.flag){
System.out.println(Thread.currentThread().getName() + "--->" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
return;
}
}
}
}*/
/**关于线程的优先级
*getPriority()获得线程优先级
*setPriority()设置线程优先级
*/
class ThreadTest09Two{
public static void main(String[] args) {
System.out.println("最高优先级:" + Thread.MAX_PRIORITY);//最高优先级:10
System.out.println("最低优先级:" + Thread.MIN_PRIORITY);//最低优先级:1
System.out.println("默认优先级:" + Thread.NORM_PRIORITY);//默认优先级:5
MyThread01 t1 = new MyThread01();
MyThread01 t2 = new MyThread01();
MyThread01 t3 = new MyThread01();
t1.setName("t1");
t2.setName("t2");
t3.setName("t3");
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t3.setPriority(Thread.NORM_PRIORITY);
t1.start();
t2.start();
t3.start();
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t1的优先级:" + t1.getPriority());//t1的优先级:10
没有合适的资源?快使用搜索试试~ 我知道了~
java高级特性,涵盖了多种java常用的一些例子
共873个文件
class:478个
java:387个
xml:6个
需积分: 0 1 下载量 174 浏览量
2023-02-12
11:25:50
上传
评论
收藏 694KB ZIP 举报
温馨提示
涉及多种线程,死锁,同步队列,异步线程,涵盖了java的自定义注解及反射,包含了23种设计模式,含注释, 线程1(基本应用) 线程2(同步,死锁,线程池) 注解和泛型 Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。 Java 语言中的类、方法、变量、参数和包等都可以被标注。Java标注可以通过反射获取标注内容。在编译器生成类文件时,标注可以被嵌入到字节码中。Java 虚拟机可以保留标注内容,在运行时可以获取到标注内容 。 Java 注解用于为 Java 反射机制 在运行状态中,对于任意一个类能够获取类中的所有属性和方法,对于任意一个对象能够调用对象中的属性和方法,这种动态获取属性和方法的机制:反射。 设计模式1(单例,工厂) 设计模式2(观察者,适配器) 设计模式(Design Pattern)是前辈们对代码开发经验的总结,是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结,是解决特定问题的一系列套路。它不是语法规定,而是一套用来提高代码可复用性、可维护性、可读性、稳健性以及安全性的解决方案
资源推荐
资源详情
资源评论
收起资源包目录
java高级特性,涵盖了多种java常用的一些例子 (873个子文件)
BaseDao.class 6KB
BaseDaoNew.class 6KB
T09_lock_condition.class 3KB
PageBean.class 3KB
JDKProxy.class 3KB
F0_F5.class 3KB
day3.class 3KB
Demo3.class 3KB
day2.class 3KB
JDKProxy.class 3KB
T07_sync_wait_notify.class 3KB
Goods_In.class 3KB
Goods_In.class 3KB
CglibProxy.class 3KB
da4.class 3KB
Demo4.class 3KB
Goods_info.class 3KB
CglibProxy.class 2KB
Demo2.class 2KB
da2.class 2KB
T11_TransFerQueue.class 2KB
F0_F4$Worker.class 2KB
F0_F4$Boss.class 2KB
ThreadTest.class 2KB
Person_day.class 2KB
Person.class 2KB
day1.class 2KB
Student.class 2KB
Waitda1.class 2KB
MyaJbdc.class 2KB
T0_F2.class 2KB
T06_sync_wait_notify.class 2KB
TableJdbc.class 2KB
Callableda1.class 2KB
Prototype.class 2KB
Demo2.class 2KB
T0_F3$Boss.class 2KB
WaitDemo.class 2KB
T08_lock_condition.class 2KB
Prototype.class 2KB
Student.class 2KB
ConnectionPool.class 2KB
ConnectionPool.class 2KB
TicketsDemo1.class 2KB
Ticketsda1.class 2KB
Test.class 2KB
StrategyTest.class 2KB
T02_00_LockSupport.class 2KB
F0_F5$MyTask.class 2KB
AnnotationTest_day.class 2KB
Book.class 2KB
Books.class 2KB
Demo1.class 2KB
AnnotationTest.class 2KB
TableJdbcNew.class 2KB
Inter_da1.class 2KB
Inter_Test.class 2KB
ThreadTest22$Account.class 2KB
Book_Two.class 2KB
T0_F3$Worker.class 2KB
ThreadTest16$Consumer.class 2KB
DeadLock$Philosohper.class 2KB
Goods.class 2KB
DeadLockNew$PhilosoHper.class 2KB
ConsumerTow.class 2KB
ThreadTest13Tow.class 2KB
ProducerTwo.class 2KB
ThreadTest09Two.class 2KB
TreeNode.class 2KB
ThreadTest16$Producer.class 2KB
AbstractSubject.class 2KB
ThreadTest20$Account02.class 2KB
ThreadTest22$AccountTwo.class 2KB
Context.class 2KB
da3.class 2KB
TreeNode.class 2KB
Test_day.class 2KB
Demo3.class 2KB
ThreadTest19$AccountRunnable01.class 2KB
ThreadSafe01$AccountRunnable01.class 2KB
ThreadTest21$AccountRunnable03.class 2KB
ThreadTest20$AccountRunnable02.class 2KB
Join_da1.class 2KB
Join_Test.class 2KB
ThreadTest23$MyRunnable2C.class 2KB
ThreadTest24$MyRunnable2C.class 2KB
ThreadTest22$AccountTwo$AccountThread.class 2KB
AbstractSubject.class 1KB
ThreadTest23$MyRunnable1C.class 1KB
ThreadTest24$MyRunnable1C.class 1KB
T0_F2$MyTask.class 1KB
Person_day$Person_dayBuilder.class 1KB
Test.class 1KB
Account02.class 1KB
ThreadTest11.class 1KB
Threadda2.class 1KB
Person$PersonBuilder.class 1KB
ThreadDemo2.class 1KB
Daemonda1.class 1KB
DaemonDemo.class 1KB
共 873 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论
id_lian
- 粉丝: 155
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功