package org.kne.cloud.network;

import java.lang.reflect.Method;

public class ThreadTool {
	public static boolean forceD=true;
	public static boolean first=true;
	public static Thread makeVThreadIfSupport(String name,Runnable r) {
		if(forceD)
			return new Thread(r, name);
		try {	//return new Thread(r, name);
			Class<?> c=Class.forName("java.lang.Thread");
			Method m= c.getDeclaredMethod("ofVirtual", null);
			Object o=m.invoke(null, null);
			Class<?> cb=Class.forName("java.lang.Thread$Builder");
			Method mn=	cb.getDeclaredMethod("name", String.class);
			mn.invoke(o, name);
			Method mu=	cb.getDeclaredMethod("unstarted", Runnable.class);
			return (Thread) mu.invoke(o, r);
			//return Thread.ofVirtual().name(name).unstarted(r);
		}catch(Throwable e) {
			//e.printStackTrace();
			if(first) {
			System.out.println("提示：请使用java21以上版本以提高本软件的数据转发性能！");
			first=false;
			}
			return new Thread(r, name);
		}
	}
	public static Thread makeVDaemonThreadIfSupport(String name,Runnable r) {
		if(forceD) {
			Thread rt=new Thread(r, name);
			rt.setDaemon(true);
			return rt;
		}
		try {	//return new Thread(r, name);
			Class<?> c=Class.forName("java.lang.Thread");
			Method m= c.getDeclaredMethod("ofVirtual", null);
			Object o=m.invoke(null, null);
			Class<?> cb=Class.forName("java.lang.Thread$Builder");
			Method mn=	cb.getDeclaredMethod("name", String.class);
			mn.invoke(o, name);
			Method mu=	cb.getDeclaredMethod("unstarted", Runnable.class);
			return (Thread) mu.invoke(o, r);
			//return Thread.ofVirtual().name(name).unstarted(r);
		}catch(Throwable e) {
			//e.printStackTrace();
			if(first) {
			System.out.println("提示：请使用java21以上版本以提高本软件的数据转发性能！");
			first=false;
			}
			Thread rt=new Thread(r, name);
			rt.setDaemon(true);
			return rt;
		}
	}

	public static Thread makePThreadIfSupport(String name, Runnable r) {
		Thread rt=new Thread(r, name);
		return rt;
	}
	public static Thread makePDaemonThreadIfSupport(String name, Runnable r) {
		Thread rt=new Thread(r, name);
		rt.setDaemon(true);
		return rt;
	}
	public static Thread makeVDaemonThread(String name, Runnable r) {

		if(false)
		return new Thread(r, name);
		try {	
			Class<?> c=Class.forName("java.lang.Thread");
			Method m= c.getDeclaredMethod("ofVirtual", null);
			Object o=m.invoke(null, null);
			Class<?> cb=Class.forName("java.lang.Thread$Builder");
			Method mn=	cb.getDeclaredMethod("name", String.class);
			mn.invoke(o, name);
			Method mu=	cb.getDeclaredMethod("unstarted", Runnable.class);
			return (Thread) mu.invoke(o, r);
			//return Thread.ofVirtual().name(name).unstarted(r);
		}catch(Throwable e) {
			//e.printStackTrace();
			if(first) {
			System.out.println("提示：请使用java21以上版本以提高本软件的数据转发性能！");
			first=false;
			}
			Thread rt=new Thread(r, name);
			rt.setDaemon(true);
			return rt;
		}
	}
	public static Thread makeVThread(String name,  Runnable r) {
		if(false)
		return new Thread(r, name);
		try {	//return new Thread(r, name);
			Class<?> c=Class.forName("java.lang.Thread");
			Method m= c.getDeclaredMethod("ofVirtual", null);
			Object o=m.invoke(null, null);
			Class<?> cb=Class.forName("java.lang.Thread$Builder");
			Method mn=	cb.getDeclaredMethod("name", String.class);
			mn.invoke(o, name);
			Method mu=	cb.getDeclaredMethod("unstarted", Runnable.class);
			return (Thread) mu.invoke(o, r);
			//return Thread.ofVirtual().name(name).unstarted(r);
		}catch(Throwable e) {
			//e.printStackTrace();
			if(first) {
			System.out.println("提示：请使用java21以上版本以提高本软件的数据转发性能！");
			first=false;
			}
			return new Thread(r, name);
		}
	}
}
