[英]Java class that starts a new thread in main class
I'm trying to write a library for my users, and would like it to be as easy to use as possible. 我正在尝试为我的用户编写一个库,并希望它尽可能地易于使用。 Right now, to use it, you have to start a new thread, and set a lot of variables to track, in your main class, however my users are not always experienced in Java, and threading might be too hard for some of my users. 现在,要使用它,您必须在您的主类中启动一个新线程,并设置很多要跟踪的变量,但是我的用户并不总是具有Java经验,因此对于某些用户而言,线程化可能太难了。
So I want to move the entire threading into my class, and allow users to call a function, to start the thread. 所以我想将整个线程移到我的类中,并允许用户调用一个函数来启动线程。
Here is the thread I'm currently running in the main class: 这是我当前在主类中运行的线程:
int valueToAdd = 0;
DreamStatsTracker tracker = new DreamStatsTracker();
StartSessionResponse response = tracker.StartSession("ae-13s-90-11", "Abe");
System.out.println(response.getMessage());
System.out.println("Adding skill Herblore");
String message = tracker.AddSkill(response.getSessionId(), Skill.HERBLORE, "Abe");
Thread thread = new Thread(() -> {
while(true) {
tracker.SetValueForSkill(response.getSessionId(), Skill.HERBLORE, valueToAdd);
}
});
I would like to move that entire thing into the DreamStatsTracker
class, and do something like this from the main class: 我想将整个内容移入DreamStatsTracker
类,并从主类中执行以下操作:
DreamStatsTracker tracker = new DreamStatsTracker();
tracker.AddSkill(Skill1, ValueForSkill1);
tracker.AddSkill(Skill2, ValueForSkill2);
tracker.Start();
This would start a new instance of the DreamStatsTracker
, which would start a new thread, and track the values of the added skills continuously in the background, while the main class is running. 这将启动DreamStatsTracker
的新实例,该实例将启动新线程,并在主类运行时在后台连续跟踪所添加技能的值。
How would I approach this, and is it even possible? 我将如何处理,甚至可能?
I finally solved my issue, by hours of testing and trying. 经过数小时的测试和尝试,我终于解决了我的问题。
This is what I did: 这是我所做的:
Tracker class: 追踪器类别:
Thread mainThread;
public DreamStatsTracker(){
mainThread = new Thread(() -> {
while(true){
for (SkillAndValue skillAndValue : Skills) {
try {
SetValueForSkill(this.SessionId, skillAndValue.getSkill(), skillAndValue.getValue());
System.out.println("Added value: " + skillAndValue.getValue());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void AddSkillAndValue(Skill Skill, Callable<Integer> function) throws Exception{
Skills.add(new SkillAndValue(Skill, function));
}
public void Start(){
for (SkillAndValue skillAndValue : Skills) {
AddSkill(this.SessionId, skillAndValue.getSkill(), this.Username);
}
this.mainThread.start();
}
This is the main class: 这是主要的类:
public static void main(String[] args) throws Exception {
DreamStatsTracker tracker = new DreamStatsTracker();
tracker.StartNewSession("ae-13s-90-11", "Abe");
tracker.AddSkillAndValue(Skill.HERBLORE, getRandomNumber());
tracker.Start();
}
private static Callable<Integer> getRandomNumber(){
return new Callable<Integer>(){
public Integer call(){
Random random = new Random();
return random.nextInt(10000);
}
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.