java - Read Field from another Thread -
i i'm creating textbased game while i'm learning java. i'm having issue when i'm trying read field thread. sleep class:
package events; public class sleep implements runnable { public int sleep = 100; public void run() { while (true) { sleep--; system.out.println("sleep: " + sleep); try { thread.sleep(1000); } catch (interruptedexception e) { e.printstacktrace(); } if (sleep == 50) { system.out.println("you need eat"); } if (sleep == 25) { system.out.println("you realy need eat"); } if (sleep == 10) { system.out.println("you'r almoust dying go eat"); } if (sleep == 0) { system.out.println("you'r dead"); } } } public void printsleep() { system.out.println("sleep: " + sleep); } }
then call method "printsleep" menu class , should appear sleep: 99 appear sleep: 100
menu class:
public class menu { hunger hunger = new hunger(); sleep sleep = new sleep(); scanner scanner = new scanner(system.in); string answer; public void mainmenu(){ system.out.println("main menu: 1 home, 2 work, 3 shop, 4 nessecity"); answer = scanner.next(); if(answer.equals("1")){ homemenu(); } else if(answer.equals("2")){ workmenu(); } else if(answer.equals("3")){ shopmenu(); } else if(answer.equals("4")){ hunger.printhunger(); sleep.printsleep(); } }
edit: sorry o forgot put classes. human class:
public class human{ hunger hunger = new hunger(); thread threadhunger = new thread(hunger); sleep sleep = new sleep(); thread threadsleep = new thread(sleep); menu menu = new menu(); scanner scanner = new scanner(system.in); private string name; int money = 100; public void createcharacter() { system.out.println("type name:"); name = scanner.next(); try { thread.sleep(500); } catch (exception e1) { e1.printstacktrace(); } system.out.println("your profile been created " + name + " name , " + money + " money.\n"); try { thread.sleep(500); } catch (exception e2) { e2.printstacktrace(); } threadhunger.start(); threadsleep.start(); menu.mainmenu(); } }
main class:
public class game { static human human = new human(); public static void main(string[] args) { system.out.println("to start game create human"); human.createcharacter(); } }
what happens here in menu
class, creating new hunger
, sleep
objects, separate , have nothing hunger
, sleep
objects created , ran inside human
class.
so creating human , running hunger
, sleep
through threads, menu doesn't see these hunger
, sleep
. sees ones created itself, , not running.
when create menu
object, should pass hunger
, sleep
objects human
has access them. need change menu
this:
public class menu { private final hunger hunger; // not initialized here in constructor private final sleep sleep; scanner scanner = new scanner(system.in); string answer; /** * constructor initializes internal sleep , hunger * values passed whoever creating , using it. * * @param hunger hunger object passed calling human * @param sleep sleep object passed calling human */ public menu( hunger hunger, sleep sleep ) { this.hunger = hunger; this.sleep = sleep; } public void mainmenu(){ system.out.println("main menu: 1 home, 2 work, 3 shop, 4 nessecity"); answer = scanner.next(); if(answer.equals("1")){ homemenu(); } else if(answer.equals("2")){ workmenu(); } else if(answer.equals("3")){ shopmenu(); } else if(answer.equals("4")){ hunger.printhunger(); sleep.printsleep(); } } }
now have menu
class can receive hunger
, sleep
object, need change following line human
:
menu menu = new menu();
to:
menu menu = new menu(hunger, sleep);
now menu created has access hunger
, sleep
objects created inside human
.
but change sleep
inside sleep
volatile
or menu
might not show correctly!
public volatile int sleep = 100;
if have similar variable in hunger
, same there.
note: changed name of method mainmenu
instead of mainmenu
, names of other methods called inside it. method names , variable names should start lowercase letter. type names (classes, interfaces, enums) supposed start uppercase letter.
Comments
Post a Comment