android - How to work with a Class that Implements Runnable in Java -
i not new threads, use threads run long processes, create instance of runnable
class , put code want run in run()
method, yesterday lecturer gave me task create simple program implements runnable
class, class demonstrate how use start(), wait(), sleep(), join() , yield()
method calls, said every time want run thread create instance of runnable
class , add handler
or looper
implementing class have never done before bit lost, choose create simple contactbook program, program save , delete contacts file, 1 of these classes should implement runnable
class, choose class saves , loads contacts file because part might take more time hence needs run on separate thread below classes
person class (a blueprint of data each contact should have)
public class person implements runnable{
private string fname; private string lname; private string number; private string calltime; public person() { this("", "","",""); } public person(string fname, string lname, string number, string calltime) { this.fname = fname; this.lname = lname; this.number = number; this.calltime = calltime; } public string getfname() { return fname; } public void setfname(string fname) { this.fname = fname; } public string getlname() { return lname; } public void setlname(string lname) { this.lname = lname; } public string getnumber() { return number; } public void setnumber(string number) { this.number = number; } public string getcalltime() { return calltime; } public void setcalltime(string calltime) { this.calltime = calltime; } public void printcontacts() { timeago timesaved = new timeago(); system.out.println("-------------------------------------"); system.out.println(" 1. first name: "+this.fname); system.out.println(" last name: "+this.lname); system.out.println(" cell number: "+this.number); system.out.println(" saved date: "+string.valueof(timesaved.gettimeago(long.parselong(this.calltime)))); system.out.println("-------------------------------------"); }
}
contactmanager class, class responsible writing, reading , deleting contacts file, readallcontacts , writetofile
methods ones want run on thread, because might take bit of time reading , writing. question how can tell contactmanager
run 2 methods on separate thread without need of creating instance of runnable
class , surrounding method bodies in run()
method?
Comments
Post a Comment