Thread(上) 【015】

本文介绍了Java中创建和管理线程的多种方法,包括继承Thread类和实现Runnable接口两种常见方式,还探讨了线程的中断、同步、优先级设置、停止及yield方法的使用。
//、、、、、[color=red]创建线程的方法一[/color]、、、、、、、、、、、、、、、、、、、\\

[code]
package com.testthread;

public class TestThread2 {
public static void main(String args[]) {
Runner2 r = new Runner2() ;
r.start() ;
for(int i=0; i<100; i++) {
System.out.println("Main thread:-----" + i) ;
}
}
}
class Runner2 extends Thread { //通过继承Thread 类创建线程
public void run() {
for(int i=0; i<30; i++) {
System.out.println("Runner2:--" + i) ;
}
}
}
[/code]

//、、、、、[color=red]创建线程的方法二[/color]、、、、、、、、、、、、、、、、、、、\\
[code]
package com.testthread; //默认导入java.lang包
public class TestThread1 {
public static void main(String args[]) {
Runner1 r = new Runner1() ;
Thread th = new Thread(r) ;
th.start(); //线程启动
r.run(); //这个是方法调用,不能使新线程启动

for(int i=0; i<100; i++) {
System.out.println("Main thread:-----" +i) ;
}
}
}
class Runner1 implements Runnable { // 通过实现runnable接口创建和启动线程,尽量使用该方法而少用(不用)继承Thread方法
public void run() {
for(int i=0; i<30; i++) {
System.out.println("Runner1------"+i) ;
}
}
}
[/code]
//。。。。。[color=red]TestInterrupted[/color]。。。。。。。。。。。。。。。。。。\\
[code]
package com.testthread;
import java.util.* ;
public class TestInterrupted {
public static void main(String args[]) {
Thread th = new Thread(new MyThread()) ;
th.start();
try {
Thread.sleep(10000) ; //该sleep 是让Main 线程休眠10秒,让th 线程工作
} catch (InterruptedException e) {
e.printStackTrace();
}
th.interrupt(); //中断线程,MyThread 里的catch
}
}
class MyThread implements Runnable {
public void run() {
while(true) {
System.out.println("===" + new Date() +"===") ; //输出系统时间,输出十次,因为main休眠10秒
try {
Thread.sleep(1000); // sleep 是static 属性,可以直接调用。让该线程休眠1秒
} catch (InterruptedException e) {
return; // 当休眠被打断时停止
}
}
}
}
[/code]

//。。。。。[color=red]TestJoin[/color]。。。。。。。。。。。。。。。。。。\\
[code]
package com.testthread;

public class TestJoin {
public static void main(String[] args) {
MyThread1 m = new MyThread1("New") ;
m.start();
try {
m.join(); //等待m线程终止才继续往下进行,和调用run方法结果一样
} catch (InterruptedException e) {
e.printStackTrace();
}

for(int i=1; i<=10; i++) {
System.out.println("This is main thread.") ;
}
}
}
class MyThread1 extends Thread {
MyThread1(String s) {
super(s) ; //调用Thread中的构造方法
}

public void run() {
for(int i=1; i<=10; i++) {
System.out.println("This is" + getName()); //返回该线程的名称
try {
sleep(1000) ;
} catch (InterruptedException e) {
return ;
}
}
}
}
[/code]


//。。。。。[color=red]TestPriority[/color]。。。。。。。。。。。。。。。。。。\\
[code]
package com.testthread;

public class TestPriority {
public static void main(String args[]) {
Thread t1 = new Thread(new MyThread3()) ;
Thread t2 = new Thread(new MyThread4()) ;
t1.setPriority(Thread.NORM_PRIORITY + 3) ; //Thread.NORM_PRIORITY---分配给线程的默认优先级
t1.start() ;
t2.start() ;
}
}

class MyThread3 implements Runnable {
public void run() {
for(int i=0; i<100; i++) {
System.out.println("MyThread3: " + i) ;
}
}
}

class MyThread4 implements Runnable {
public void run() {
for(int i=0; i<100; i++) {
System.out.println("-------Mythreads4: " + i) ;
}
}
}
[/code]

//。。。。。[color=red]TestStop[/color]。。。。。。。。。。。。。。。。。。\\

[code]
package com.testthread;

public class TestStop {
public static void main(String args[]) {
ThreadRun tr = new ThreadRun() ;
Thread r = new Thread(tr);
r.start();
for (int i = 0; i < 1000000; i++) {
if (i % 1000 == 0) {
System.out.println("in thread main i=" + i);
}
}
System.out.println("Thread main is over");
tr.shutDown() ;
r.stop();
}
}

class ThreadRun implements Runnable {
private boolean flag = true;

public void run() {
int i = 0 ;
while (flag = true) {
System.out.print(" " + i++);
}
}

public void shutDown() {
flag = false ;
}
}

[/code]

//。。。。。[color=red]TestYield[/color]。。。。。。。。。。。。。。。。。。\\

[code]
package com.testthread;
public class TestYield {
public static void main(String args[]) {
MyThread2 th1 = new MyThread2("th1") ;
MyThread2 th2 = new MyThread2("th2") ;
th1.start();
th2.start();
}
}

class MyThread2 extends Thread {
public MyThread2(String s) {
super(s) ;
}

public void run() {
for(int i=1; i<=100; i++) {
System.out.println(getName()+ ": "+i) ;
if(i%10 == 0) {
yield() ; //当i值是10的倍数时候,暂停当前正在执行的线程对象,并执行其他线程
}
}
}
}

[/code]
``` #include "udf.h" #include "sg_mphase.h" #include "math.h" /******************** 用户自定义参数 ********************/ #define C_i 0.25 // Lee模型松弛因子 #define h_fg 452e3 // 汽化潜热 [J/kg] #define T_in 18.7 // 入口液氢温度 [K] #define m_dot_liq 0.015 // 液氢质量流量 [kg/s] #define P_initial 26889.72 // 初始罐压 [Pa] #define T_initial 71.0 // 初始气相温度 [K] /******************** NIST高精度物性拟合 ********************/ // 液氢饱和温度计算 (误差<0.3K) real T_SAT(real P) { return 14.12 * pow(P/1e3, 0.12) - 1.8e-4*pow(P/1e3,2); // 适用于10-100kPa } // 液氢饱和压力计算 (误差<1.5%) real P_SAT(real T) { return 1e3 * exp(12.46 - 98.34/(T + 2.78)); // 修正Antoine方程 } /******************** 入口闪蒸模型 ********************/ DEFINE_PROFILE(inlet_flash_profile, thread, position) { face_t f; real P_tank = RP_Get_Real("operating-pressure"); // 绝对压力 real P_sat_in = P_SAT(T_in); // 入口温度对应饱和压 begin_f_loop(f, thread) { real phi = 0.0; if (P_tank < P_sat_in) { // 基于热力学平衡的闪蒸率 real h_in = 160e3; // 18.7K液氢比焓 [J/kg] real h_v_sat = h_in + h_fg; phi = (h_v_sat - h_in)/(h_fg); phi = MIN(MAX(phi,0.0),0.9); // 闪蒸率限制在90%以内 } F_PROFILE(f, thread, position) = m_dot_liq * (1 - phi); } end_f_loop(f, thread) } /******************** 增强相变模型 ********************/ DEFINE_SOURCE(liq_source, cell, thread, dS, eqn) { real T = C_T(cell, thread); real P = C_P(cell, thread); real T_sat = T_SAT(P); real alpha_liq = C_VOF(cell, thread, LIQUID_PHASE); real rho_liq = C_R_M1(cell, thread); // 液相密度 if (alpha_liq > 0.01 && T > T_sat) { // 蒸发条件 real S = -C_i * pow(alpha_liq,0.7) * rho_liq * (T - T_sat)/T_sat; dS[eqn] = -C_i * rho_liq/T_sat; return S; } else if (alpha_liq < 0.99 && T < T_sat) { // 冷凝条件 real S = C_i * pow(1-alpha_liq,1.2) * rho_liq * (T_sat - T)/T_sat; dS[eqn] = -C_i * rho_liq/T_sat; return S; } return 0.0; } DEFINE_SOURCE(vap_source, cell, thread, dS, eqn) { return -liq_source(cell, thread, dS, eqn); } DEFINE_SOURCE(energy_source, cell, thread, dS, eqn) { real S = liq_source(cell, thread, dS, eqn); return S * h_fg; } /******************** 初始条件设置宏 ********************/ DEFINE_INIT(init_cond, domain) { Thread *thread; cell_t cell; // 设置初始压力场 real operating_pressure = RP_Get_Real("operating-pressure"); // 遍历所有流体单元 thread_loop_c(thread, domain) { if (FLUID_THREAD_P(thread)) { begin_c_loop(cell, thread) { // 设置初始压力(表压) C_P(cell, thread) = P_initial - operating_pressure; // 设置初始温度 C_T(cell, thread) = T_initial; // 设置初始相分数(全为气相) C_VOF(cell, thread, LIQUID_PHASE) = 0.0; C_VOF(cell, thread, VAPOR_PHASE) = 1.0; } end_c_loop(cell, thread) } } Message("Initialized: P=%.2f Pa, T=%.1f K, Vapor=100%%\n", P_initial, T_initial); }```往罐中充液氢,初始罐中有26889.72pa的气氢,加注液氢,没有气相入口,把修改好的udf发给我,要完整的
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值